-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Champion "Allow System.Enum
as a constraint" (15.7)
#104
Comments
I'm of the opinion that Perhaps another reason I may be mistaken but I believe removing the explicit filters in the compiler preventing I would love to see this implemented as soon as possible to enable the benefits corefx#15453 would provide. I'd gladly contribute in anyway needed. |
enum
(or allow System.Enum
as a constraint)"
Generic constraints make sense with enum inheritance. Example from the future:
|
@gafter & @AnthonyDGreen is there anything myself or the community can do to make this a reality? I'd really like to move forward with corefx#15453 but this seems to be the blocker. I believe the only code changes needed would be a few line removals in roslyn/Binder_Constraints.cs. |
@TylerBrinkley I don't see what aspect of the proposed API would benefit from this. |
@gafter There are two issues with implementing corefx#15453 now without the generic
|
@gafter or @AnthonyDGreen again I ask, is there anything myself or the community can do to make this a reality? |
Forgive me but I'm going to continue to spam this request until I get a response. @gafter or @AnthonyDGreen is there anything myself or the community can do to make this a reality? |
Ditto. |
@MadsTorgersen I'm sure you get endless GitHub mentions so I apologize upfront but I was hoping you could review this. We've been trying to get a response from @gafter or @AnthonyDGreen for awhile now on how to proceed with this request but without success. I'm the author of the OSS library Enums.NET and have created a feature request in the corefx repo to include many of the enhancements my library provides directly to .NET Core. The one thing holding that request back is this missing C# language feature. Is there anything myself or the community can do to push this along? Thank you for your time. |
I snuck this into the 7.x milestone to force the LDM to triage this next week. |
Thank you so much @gafter! |
@TylerBrinkley
and T will accept any Enum that has int as underlying type That will enable to use addition or bit operations and also the code can be shared That accompanied with ref extensions methods will enable us to write any Enum flag operation possible with few generic methods
Edit: DOH!! It appears that my suggestion was first proposed by HaloFour two and a half years ago and not only that, it appears that F# already has enum generic constraints with underlying type. I am sorry about that.. |
Glad to see that after about a decade of complaints and hacked workarounds, there's finally some semblance of movement on this. I've lost count of how many times I've needed this, it's an extreme limitation when trying to do any kind of generic GUI programming. Thanks, @TylerBrinkley, for being the squeaky wheel here. |
When using generics, we have to use DynamicInvoke, boxing, and EnumParse when converting with enums. This is especially true of framework code that aims to be 'user friend;y' and let consumers define their own types. (See MVVM / RX type programming) This activity causes performance issue, especially on mobile devices. If I could implicitly cast Enums to/from int without the boxing, dynamic invocation, and conversion I would be happy.
Updated examples, my bad. |
I don't understand what you're asking. Explicitly casting to/from an enum and it's underlying integral type (which is not necessarily |
@HaloFour What helper method am I suggesting ? |
That |
|
Expliciting casting an Yes, an |
Default Interface Members will require a runtime feature and that's currently being prototyped. There's a chance that it won't happen as both teams are loathe to rev the runtime for a language feature, but that's one case where it's being considered due to a lack of options to solve the problem in the language. There is the argument that while the hood is up other minor changes might be considered. Also, IIRC, the verifier was relaxed to better support |
Note if the runtime collapses 'enum constrained' type parameters to actually just be their underlying type. That was the point i was making. It was talking about why the arbitrary 'collapsing' proposal wouldn't be acceptable
yes. this is exactly what i was proposing here: #104 (comment)
Great. All of that is still at teh CLR level. None of this involves csharplang being involved :) It's still unclear to me why there is any discussion going on here about this :) |
@panost Can you be specific: what change are you asking about for the C# language itself. Note that the C# language itself does not care about how any particular underlying platform implements generics. That's an implementation detail. There could be some platforms that literally generate a different impl for each instantiation. There could be some platforms that literally generate a single impl for all instantiations. C# doesn't care. C# just defines what you are allowed to say in C# and what the semantics are of that. If you want the actual low level implementation fo generics to be better (i.e. generating better code for enums), then that isn't under the purview of C#-lang. So, again, to repeat, here's the flowchart:
|
I want this to compile and of course be usable :) by any enum. [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetFlag(ref this T baseVal, T flag, bool value) where T : enum {
if (value) {
baseVal |= flag;
} else {
baseVal &= ~flag;
}
} |
(Did you forget a |
@panost It feels like these methods would be written extremely rarely. And there are suitable ways to write them now that have been shown to be quite fast. Are there reasons the existing solutions are not suitable? |
@yaakov-h Yes it needs a Yes these methods would be written extremely rarely, I can thing of a maximum of 20 [Flags]Enum-oriented methods, but the "technologies" behind, that used to achieve that, are going to be used extensively. For example the INumber or IBitwise "shapes" (or whatever they called now). I am not familiar with the Unsafe methods of @ufcpp, I need to investigate what exactly gets inlined. I also think that they are a solution for NETCore only. Note that @ufcpp did not include NETFramework benchmarks. But the fact remains that this implementation doesn't clearly reflect the developer intentions, or to put it otherwise is a hack to overcome a language limitation. @HaloFour I prefer to allow bitwise ops, because Enum "satisfies" (or implements) the IBitwise shape. [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetFlag<T>(ref this T baseVal, T flag, bool value) where T : IBitwise {
if (value) {
baseVal |= flag;
} else {
baseVal &= ~flag;
}
} But honestly I don't believe that any of those changes (Default Interface Members included) will come any time soon. Checking the C# history further, it appears that C# 7.3 could be used with a few external assemblies and some changes to the verifier, by NET framework 2.0 (released in 2005) without any trouble. It appears that C# lang has this limp for quite some time. |
I realize it's a bit late, now that the feature is released, but I'm wondering two things:
|
using System;
class Program
{
static void X<T>(T x) where T : Enum { }
static void Main()
{
PlatformID x = PlatformID.Win32NT;
X(x);
X<Enum>(x); // x is boxed to Enum class
}
} |
Mainly to lift the imposed limitation without precluding the possibility of more enhancements in the future.
Except for The goal was to keep it simple. Remove a line from the spec, remove a condition in the compiler and done. Anything above and beyond that would require further design and spec work. With the desire to support bitwise ops on enums this could've got lost in the quagmire. |
I see, thanks for the explanation. |
I would like to use Enum constraint with flags capabilities as well. |
I believe a flags constraint would run into the same issues as a numeric constraint. The IL instructions emitted depend on the underlying type of the enum, so you couldn't emit code for that sort of constraint today. |
This was shipped with C# 7.3, so shouldn't this issue be closed? |
@IanKemp No, we only close "Champion" issues that we decide not to do. This one is assigned the 7.3 milestone because it was completed in C# 7.3. |
@gafter should it be marked at completed somehow? Because I feel a bit confusing as well to have open proposal for completed features |
Closed issues are harder to find on GitHub because the issue search includes I think the intent is that the tag "C# 7.3" tells you when the feature will be/was released. |
Agree
Not all knows and can understand, is 7.3 already released or not. I think, marking as checked "Finalized (done in 7.3)" in start topic is enough for mark issue implemented. |
For general developers working in C#, I tend to agree with you - the average developer cares more about getting their day to day job done than anything else. But ... ... here, in the charplang repo, practical discussion of the evolution of C# is the whole reason we're here. Those who choose tohang out here (even those who just lurk, silently) are, by definition, those who care about such things - and the answer is not hard to find. |
I use it in release version of C#. Should it be closed? |
Current policy AFAIK, is that issues remain open until they are included in an updated spec. |
Regarding the recent availability of Generic math preview, it would make sense we also add something along the lines of |
This issue was implemented in C# 7.3. If you have a different proposal, please open a new discussion. |
@333fred shouldn't this item be closed? I don't mean to dictate anything, just figured it might be one of those things that fell through the cracks. |
IIRC the issue remains open until the proposal is adopted into the current specification. |
That is correct. The |
Allows
where T : System.Enum
See also dotnet/roslyn#262
The text was updated successfully, but these errors were encountered: