1

I was checking in the c# 5,6 specs and is not clear if it is possible use a undefined number of types, such as an array, in the signature of a Generic class definition. As it is very know yu can use out paramts <Generics>[] inputArrays for the constructor signature or any function and the compiler points each parameter to any position in the params array.

//Using only a genereic type from A class
Foo<A> fooA = new Foo<A>(new A());
//Using two genereic types A and B.
Foo<A,B> fooAB = new Foo<A,B>(new A() , new B() );
//Using three genereic types A ,B and C
Foo<A,B,C> fooABC = new Foo<A,B,C>(new A() , new B(), new C() );
//Using n genereic types such as A , B , C , D , ... , Z
Foo<A,B,C,...,Z> fooAZ = new Foo<A,B,C,..,Z>(new A() , new B(), new C() , ... , new Z() );

The concret question is hot to set properly the correct generic signature to allow an undefined number of Types?

 //The signature<T1> only allows one Type in this case.
 //The compiler does not like something like this: T:[]
 public sealed class Foo<T> {
      public Foo<T>(out params T[] args) {

      }
 }

To avoid this question will be wrong closed I attache the argument that validates that c# could allow it:

using the grammar of csharp

type_parameter_constraints_clauses
    : type_parameter_constraints_clause
    | type_parameter_constraints_clauses type_parameter_constraints_clause
    ;
    
type_parameter_constraints_clause
    : 'where' type_parameter ':' type_parameter_constraints
    ;

type_parameter_constraints
    : primary_constraint
    | secondary_constraints
    | constructor_constraint
    | primary_constraint ',' secondary_constraints
    | primary_constraint ',' constructor_constraint
    | secondary_constraints ',' constructor_constraint
    | primary_constraint ',' secondary_constraints ',' constructor_constraint
    ;

primary_constraint
    : class_type
    | 'class'
    | 'struct'
    ;

secondary_constraints
    : interface_type
    | type_parameter
    | secondary_constraints ',' interface_type
    | secondary_constraints ',' type_parameter
    ;

constructor_constraint
    : 'new' '(' ')'
    ;

In according to the standard. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#1425-type-parameter-constraints

in the part (1425 of the c# specification) says:

"If C is an array type E[] then Cₓ is the array type Eₓ[]."

it means that you must be use something like :

class Foo<T[]>

Where is the interpretation error in the c# grammar?

Fabio Andrés
  • 229
  • 2
  • 11
  • 2
    On the contrary, it's clear from the docs that you can't have arbitrary type parameters. Nothing in the docs suggests that you can. What are you trying to do? Why do you think you need arbitrary types? – Panagiotis Kanavos Jan 25 '22 at 23:01
  • 1
    Does this answer your question? [How to create Generic class that takes unlimited type](https://stackoverflow.com/questions/35405693/how-to-create-generic-class-that-takes-unlimited-type) – madreflection Jan 25 '22 at 23:04
  • 1
    C# generics aren't C++ templates. Idioms used in one language don't translate to another one, with different semantics. Unlike C++, C# generic types and methods exist at the runtime and IL level, they don't get replaced. – Panagiotis Kanavos Jan 25 '22 at 23:11
  • I'm not even sure why you would want to do this. How could you use an arbitrary-length list of types? What purpose would the types serve? You cannot define an array of different types, and you cannot define an object of variable quantity of fields, so there isn't any code that would be aided by such a feature – Charlieface Jan 25 '22 at 23:22
  • 2
    Depending on what you really want to do, you may find it helpful to look at how Microsoft implemented `Tuple`: https://learn.microsoft.com/en-us/dotnet/api/system.tuple-8?view=net-6.0 – Jack A. Jan 25 '22 at 23:24
  • The main idea is that the compiler directly point to a type instead of to use an object array where you perform boxing and unboxing. – Fabio Andrés Jan 25 '22 at 23:26
  • 1
    You're asking about generic type parameters, yet the grammar you've posted is about generic type constraints. They are not the same thing. – Enigmativity Jan 25 '22 at 23:33
  • Also, the specification allows for an arbitrary number of parameters, but it doesn't allow for specifying an arbitrary number of parameters. There is a distinction. – Enigmativity Jan 25 '22 at 23:38
  • Here's the actual specification: `type_parameter_list : '<' type_parameters '>'; type_parameters : attributes? type_parameter | type_parameters ',' attributes? type_parameter;` – Enigmativity Jan 25 '22 at 23:41
  • Right, the comma in the syntax force the compiler to a defined static number of repetitions of types. type_parameter : identifier ; – Fabio Andrés Jan 26 '22 at 01:01
  • Thanks by the compiler level solution! – Fabio Andrés Jan 26 '22 at 01:01
  • 1
    "the comma in the syntax force the compiler to a defined static number of repetitions of types" - no, that's not true. For a number to be "static" it means unchanging. That's the opposite of what's going on here. – Enigmativity Jan 26 '22 at 03:48
  • @Enigmativity, you are right the value must be know by the compiler. Finally I have to reflect each instance of type, this is the solution. I'd like to suggest something like this of this way since the compiler you can directlly point to the N-sime reference to be a little bit fast instead of to use an array of params Type[] for example in the grammar: ´type_parameter_list := type_parameter_list(current definition) | "type_parameters[]" ´ and the compiler must to pass the reference of List of types found at runtime. I believe that it could raise bad practices to code level. – Fabio Andrés Feb 04 '22 at 16:19

0 Answers0