Support local scope for using static (to help with flag-style enums and more) #2287
Replies: 7 comments
-
Like the idea, hate the proposed implementation. It a) makes it seem as if it is related to lambdas and b) the <> syntax I feel is ugly,clunky and conflicting with generics (even if it doesn't spec-wise). I prefer the using static block idea, that is visually pleasing imho, introduces no new keywords or syntax, and is immediately clear |
Beta Was this translation helpful? Give feedback.
-
Should just allow using-namespace directives in local scope, to be honest. So: void foo() {
using static MyEnum; // As well as plain `using` for namespaces
if ((status & (Member1 | Member2 | Member3) == 0)
return;
MyMethod(Member2);
} For the second part, why not just extend It doesn't mean the same (no resources to manage here), but syntactically it matches rather well (restrict these imported symbols to this part of the code). Allowing the plain {
using Namespace;
// use namespace
}
// can't use namespace at least. |
Beta Was this translation helpful? Give feedback.
-
@johnkellyoxford / @narfanar the original post proposed, and demonstrated, two possible solutions to the problem: a A local The Lambda-style syntax, while admittedly ugly, supports both expressions and statements. For what its worth, the pre-existing Lambda syntax is (imho) similarly ugly, but I've gotten used to it over the years. My preference is for function over form, but I'd be perfectly happy with either possibility :) |
Beta Was this translation helpful? Give feedback.
-
emphasis added This is where we at once differ and agree. Sorry, I wasn't too clear about that :[ Instead of a local Your point on usability in expression context is an issue for a lot of constructs and may deserve a whole discussion for it 🌮 |
Beta Was this translation helpful? Give feedback.
-
But it is utterly unrelated to a lambda. Looking like a lambda would be wrong and confusing |
Beta Was this translation helpful? Give feedback.
-
@narfanar I'm sorry. I'm not certain I follow your point. Are you proposing that, in addition to If so, I agree. Though, to be clear the pre-existing, module-wide Another similar extension that might be fun is Though, for either of these ideas, opening a separate issue might be more appropriate. I welcome the dicussion and (if I understand you correctly) its a great idea, but I worry that it won't get the visibility it deserves buried here in the comments. |
Beta Was this translation helpful? Give feedback.
-
For the first part, this is part of #218 For the second I think there's very little chance of this happening. Lambdas mean a specific thing in C#, and adding another, completely unrelated meaning is only confusing. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This issue started life as a request to reduce code clutter when working with flag-style enums (#2282). However, some insightful comments from other contributors (@YairHalberstadt and @HaloFour) have made me more fully consider the problem.
The general problem is the repetition of C# type names when referencing static members. For me, this most often occurs with flag-style enums, but any feature addressing this problem need not be so limited.
The
using static
directive, introduced in C# 6, nicely addresses this on a module-wide basis. Upon revisiting this feature recently, it became apparent that it also, to my surprise, works forenum
members.Unfortunately, I seldom want to promote member name visibility module-wide. More often, the requirement is to promote it in a limited region of code. It would be nice if a similar solution existed that allowed this finer level of granularity.
I see two possible approaches. The first is to simply extend the syntax for the
using static
directive to a local block{ … }
.For example, assuming
MyEnum
is anenum
with membersMember1
,Member2
, andMember3
…I like this approach for its consistency, but in some circumstances it can be a bit verbose.
Instead of this, I’m intrigued by the prospect of extending the behavior of the Lamba expression operator (
=>
). Currently, parameters are specified on the left-hand side of this operator.It would be nice if (alternatively) one could specify a C# type. To avoid the possibility of naming conflicts, between types and parameter names, I propose enclosing the C# type in angle brackets
<...>
. This is similar to when a C# type is used as a generic parameter.I like this operator, because it already supports both expression and statement Lambdas. An example of an expression Lambda, adapted for this new feature, might look as follows:
Similarly, an example of a statement Lambda, adapted for this new feature, might look as follows:
Additionally, if the expression consists of constants, it should be allowed inside attribute initializers. So, for example:
Beta Was this translation helpful? Give feedback.
All reactions