IssueRaiser

Feature Proposal: Const Generics

# Const Generics "Const Generics" stands for allowing constant value to be used in a type parameter. A fully working MVP implementation for CoreCLR can be found here: https://github.com/dotnet/runtime/pull/89636 And an implementation including the managed part can be found here: https://github.com/hez2010/runtime/tree/feature/const-generics-managed Link to the language proposal: https://github.com/dotnet/csharplang/discussions/7508 ## Background and Use Cases "Const Generics" enables the use cases where developers need to pass a const value through a type parameter. Typical use cases are templating for things like shuffle (its basically a guaranteed constant) as well as for numerics, tensors, matrices and etc. For example, fixed buffer and vector types [1], jagged arrays/spans [2], constrained shape of arrays [3], numeric types and multiplier types especially in graphics programming [4], expression abstractions [5], and value specialization [6]. For [1], we can have a type `struct ValueArray<T, int N>` to define a type of array of `T` with `N` elements. This can also be useful in variadic parameters. For example, a `params ValueArray<int, 5>` can represent a variadic parameter that receives only 5 int arguments. Beside, we can also leverage the `ValueArray<T, int N>` type to implement `params {ReadOnly}Span<T>`. For [2], we can use the const type parameter to define a `Span<T, int Dim>`, so we can use `Span` for multi-dimension arrays as well. For [3], we can const

Posted by hez2010 on Jul 31, 2023, 5:13 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,000 issues0 comments

github (Software)

Feature Proposal: Const Generics

hez2010Jul 31, 2023, 5:13 PM

# Const Generics "Const Generics" stands for allowing constant value to be used in a type parameter. A fully working MVP implementation for CoreCLR can be found here: https://github.com/dotnet/runtime/pull/89636 And an implementation including the managed part can be found here: https://github.com/hez2010/runtime/tree/feature/const-generics-managed Link to the language proposal: https://github.com/dotnet/csharplang/discussions/7508 ## Background and Use Cases "Const Generics" enables the use cases where developers need to pass a const value through a type parameter. Typical use cases are templating for things like shuffle (its basically a guaranteed constant) as well as for numerics, tensors, matrices and etc. For example, fixed buffer and vector types [1], jagged arrays/spans [2], constrained shape of arrays [3], numeric types and multiplier types especially in graphics programming [4], expression abstractions [5], and value specialization [6]. For [1], we can have a type `struct ValueArray<T, int N>` to define a type of array of `T` with `N` elements. This can also be useful in variadic parameters. For example, a `params ValueArray<int, 5>` can represent a variadic parameter that receives only 5 int arguments. Beside, we can also leverage the `ValueArray<T, int N>` type to implement `params {ReadOnly}Span<T>`. For [2], we can use the const type parameter to define a `Span<T, int Dim>`, so we can use `Span` for multi-dimension arrays as well. For [3], we can const

Comments (0)

No comments yet. Start the discussion.

Comment on this issue