-
Notifications
You must be signed in to change notification settings - Fork 6k
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
C# 7.3: New forms of generic constraints unmanaged, System.Enum, and System.Delegate #3964
Comments
Isn't "blittable" constraint called |
@nietras I create placeholder tasks based on proposals. I do update the titles and notes after the syntax is finalized and before I start writing the documentation. I'll update many of the 7.3 tasks early next week, when I go through all the updated proposals, many of which now have initial implementations. |
Blittable/unmanaged needs to happen! Maybe even contraints for operators and casts? Since operators cannot be added in an interface I cannot add 2 generic Variables together. Would be cool for a generic math library where we dont want a class for every single valuetype. where T : blittable, |
The proposal has been updated and is posted here |
There are three new constraints that are enabled by this feature:
The following documents would need updates:
Add samples:
A good location for these samples is the conceptual topic for generic type constraints |
Closed in #4960 |
Is |
@IllidanS4 |
@BillWagner I don't understand the argument; at runtime, public static string Method<T>(T? arg) where T : Enum
{
return arg?.ToString();
} That cannot be expressed right now. If you wanted to pass null, you would have to make it Imagine if instead of |
Thanks for adding more to your comment @IllidanS4 I think I understand your comments better. First, if you want to propose new language features, I'd write those issues on the dotnet/csharplang repo. The language designers watch that much more closely. Here, I'll address where I might need to add more clarification: You said:
No, that's not correct. The last sentence of the spec covering generic type parameters says "The run-time execution of all statements and expressions involving type parameters uses the actual type that was supplied as the type argument for that parameter." Therefore, As for your example, this would work for excluding public static string Method<T>(T? arg) where T : struct, Enum
{
return arg?.ToString();
} You could create overloads: public static string Method<T>(T? arg) where T : struct, Enum
{
return arg?.ToString();
}
public static string Method(Enum arg)
{
return arg?.ToString();
} Overload resolution ensures that the generic method would be better than the non-generic method for any /cc @agocke to validate my explanation. |
@BillWagner I still might have not explained myself properly. However, seeing you use A similar issue is in the delegate constraint. |
I think both of what you said is correct, depending on the precise meaning of your terms. The simplest explanation I can come up with is |
There are three new forms of generic constraints being introduced.
"blittable" for unmanaged types: dotnet/csharplang#187
"enum" to specify that the type must be derived from
System.Enum
dotnet/csharplang#104"delegate" to specify that the type must derive from
System.Delegate
dotnet/csharplang#103Reading the comments, a good part of the explanation will need to be explaining the
blittable
constraint and its name. There are a lot of subtleties to the types that can be members of a blittable type.This will be new articles for the new constraint types, and updating general descriptions of generics where
class
andstruct
constraints are discussed.The text was updated successfully, but these errors were encountered: