[Proposal]: Type parameter arrays in generic functions #4415
Unanswered
wes-sleeman
asked this question in
Language Ideas
Replies: 1 comment 5 replies
-
See: dotnet/roslyn#5058 |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
TYPE PARAMETER ARRAYS IN GENERIC FUNCTIONS
Summary
C# has had parameter arrays in function calls and type parameters for a while now, but there is currently no way to pass a variable number of type parameters to a function. This proposal seeks to expand the
params
keyword to cover type parameters (only in functions for now) in addition to standard function parameters.Motivation
Many applications in parsing and meta-programming require large- or undefined-scale passing of type information. Currently, there are only two approaches to doing this: passing an
IEnumerable<Type>
object as a normal parameter (which is counter-idiomatic and is opaque to compile-time checks), or nesting type parameter declarations (which is messy, difficult to implement, difficult to document, and all around confusing (referenceSystem.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>
). Implementing type param arrays would allow for significantly cleaner and more idiomatic function definitions in a variety of domains as well as allowing for some increased compile-time type-safety checking.Detailed design
I propose we extend the
params
keyword to work in generic type specifiers. Specifically, populating the generic type parameter specified by theparams
keyword with an enumerable collection of generic type arguments such that indices of this collection can be used intuitively and resolved/checked at compile time like any other type parameters (e.g.TOthers[1] data = new();
for a generic type parameter specified with<…, params TOthers[]>
). Refer to the below examples to get a feel for how it might be used. If extended to the class-level (beyond just the basics of this proposal), this could lead to significantly simplified definitions of classes such asSystem.Tuple<params TValues[]>
orSystem.Func<TReturn, params TInputs[]>
with all the maintainability and debugging benefits that come along with it.Example usage with an array-like syntax to mimic the pre-existing function
params
format (pseudocode to demonstrate proposal, I'm aware thatreturn new TReturn() { retval };
is invalid):Drawbacks
Depending on how it's implemented, produced code may not be compliant with the current ECMA-335; refactoring this construct into a chained format of
<THead, TTail>
whereTTail
can be either a type or another<THead2, TTail2>
, etc before emitting the compiled assembly may remediate this. So long as the type-checking is implemented at the compiler level, the improved compile-time type-safety goal of this feature will be accomplished.Unresolved questions
I'm not certain how Roslyn could be convinced to accept an array of type parameters and actually do the necessary checking. Implementing a dereference of an array of types shouldn't be too hard (I'm not familiar with Roslyn specifics, but it seems like it could be within a couple of extra conditions to dereference an array out of a symbol table in a typical compiler architecture), but some additional logic may need to be implemented that allows for type-checking these manoeuvres considering that this proposal doesn't require the indices to be compile-time constants.
Beta Was this translation helpful? Give feedback.
All reactions