Proposal: Value generic parameters #3049
Replies: 7 comments
-
This has been proposed before but I can't find it on mobile. |
Beta Was this translation helpful? Give feedback.
-
I'm actually curious why this value should be passed in as a generic parameter - I really think this would be better done as a constructor parameter. |
Beta Was this translation helpful? Give feedback.
-
I do like the proposal in general, however, one would have to generate (as is the case with C++) one type per used @TonyValenti : the idea would be to provide type safety at compile-time. This would e.g. prevent someone from multiplying a |
Beta Was this translation helpful? Give feedback.
-
IMO all of the compelling use cases for such a feature (e.g. fixed length arrays) also require that the compiler or runtime spin out a different runtime type for each permutation of the generic argument. But I think this would also make them very limited and difficult to work with. Even the proposal demonstrates some kind of generic constraint to allow the compiler to perform expressions on the generic type arguments to derive the value of generic type arguments at compile-time, which brings generics into the world of templates in some weird hybrid way. |
Beta Was this translation helpful? Give feedback.
-
There have been a couple of other similar proposals opened, they all seem closed now: |
Beta Was this translation helpful? Give feedback.
-
FWIW, doesn't Rust have this, specifically for arrays? It's the only instance of dependent types in ithe language but While I love this idea, you'd need to go a lot further than just allowing a value as a type parameter to make it useful; you'd need to build some arithmetic into the compiler. Otherwise, for example, arrays would need to be exactly the right size to be assignable, rather than just at least the right size. |
Beta Was this translation helpful? Give feedback.
-
Fixed-size buffers would definitely improve with this, but implementing this properly is a very tedious task. Even for this simple type: class Constant<int V>
{
public const int Value = V;
} If the type shall be truly generic (and not just a template), this would mean that every integer has to be encoded as a type, in order not to break every code that does reflection with generics. This means there has to be both an agreed-upon method to encode bytes into an unique type, but also a way to query a value of such a type in CIL. For a const field, the encoding would be modified. Even for a fixed-size buffer, its size is controlled via the Without modifying the encoding, the runtime could be modified to allow a special type There are also other places where this seems to be logical but would be quite hard to get into, like arguments of attributes or default values of parameters. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I suggest add to C# and CLR support of value generic parameters. Small example:
And example of usage:
Of course, all generic parameters must be resolved in compile time, so all values, that can be passed as value generic parameter must be declared as constants. This limits the possible types that can be used as value generic parameters.
Beta Was this translation helpful? Give feedback.
All reactions