Proposal discussion: Target-typed static member lookup #9078
Replies: 8 comments 16 replies
-
I'm personally not a fan of the "dot" syntax. I'd much prefer |
Beta Was this translation helpful? Give feedback.
-
Overall, this doesn't promote readability and quick comprehension of what the code is doing. However, if this does become a language feature I think this should be restricted to Enum types. Also, why bother with the using static System.Drawing.Color;
control.ForeColor = Red; |
Beta Was this translation helpful? Give feedback.
-
Initially I disliked it as well but I think I can get used to it pretty easily. :D |
Beta Was this translation helpful? Give feedback.
-
Great proposal @jnm2; when this was originally discussed I never wanted the leading dot but I think your explanation in the proposal has won me over, especially if the feature's going to be applicable to any static member with the correct type. |
Beta Was this translation helpful? Give feedback.
-
Isn't it a problem that |
Beta Was this translation helpful? Give feedback.
-
The specification is ready for LDM and committed at proposals/target-typed-static-member-lookup.md. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Is this a new organization pattern you guys are doing from now on? I assume this is due to the fact that discussions have threads capabilities whereas the issue is flat (so there is a substantial benefit on keeping the discussion here?). |
Beta Was this translation helpful? Give feedback.
-
Example of the feature using both unary and binary operators and both regular expressions and patterns: foreach (var property in type.GetProperties(.Public | .NonPublic | .Static | .Instance | .DeclaredOnly))
{
if (property.SetMethod is not { } setter)
{
continue;
}
if ((setter.Attributes & .MemberAccessMask) is not (.Public or .Family or .FamORAssem))
{
continue;
}
// ...
} Instead of today's: foreach (var property in type.GetProperties(
BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Static | BindingFlags.Instance
| BindingFlags.DeclaredOnly))
{
if (property.SetMethod is not { } setter)
{
continue;
}
if ((setter.Attributes & MethodAttributes.MemberAccessMask) is not (
MethodAttributes.Public or MethodAttributes.Family or MethodAttributes.FamORAssem))
{
continue;
}
// ...
} |
Beta Was this translation helpful? Give feedback.
-
Specification: proposals/target-typed-static-member-lookup.md
Champion issue: #9138
Summary
This feature enables a type name to be omitted from static member access when it is the same as the target type.
This reduces construction and consumption verbosity for factory methods, nested derived types, enum values, constants, singletons, and other static members. In doing so, the way is also paved for discriminated unions to benefit from the same concise construction and consumption syntaxes.
Beta Was this translation helpful? Give feedback.
All reactions