Question

Please note that I am not talking about Generic Type parameters.

For example in Ada I can write a package that needs to be initialized using a value instead of a type. e.g.

generic
  Size : Positive;
package Sudokus is

   subtype Values is Positive range 1..Size*Size;
   type Choice is private;
   type Sudoku is private;

   procedure Fill(S : out Sudoku);
   procedure Init(S : out Sudoku);
   procedure Solve(S : in out Sudoku);
   procedure Print(S : Sudoku);

   Unsolvable_Grid_Error : exception;

And this is how to use it :

package My_Sudoku is new Sudokus(3); -- 3 for 9x9 solver, 4 for 16x16 solver, etc.

I guess there is no equivalent but I find it quite useful. Is there a reason for this absence ?

Was it helpful?

Solution

The general answer to "why does C# not have feature X" revolves around the benefits of such a feature versus the costs. Benefits are usually obvious, but costs include:

  • Increased complexity of language specification
  • Increased complexity of code using the language to the full
  • Increased educational requirement - developers have to know more in order to understand their colleagues' code
  • Cost of designing the feature
  • Cost of implementing the feature
  • Cost of testing the feature
  • Increased difficulty in adding more features later - because often features will interact with each other, often in awkward ways

Basically, it shouldn't be a case of asking why a particular feature isn't present: it should be a matter of arguing that the benefits of the feature are enormous compared with the costs. Features have to earn their place in the language, and the language designers have set the bar pretty high. (As Anders has put it in the past, every feature starts out with a score of -100, and has to work its way up.)

OTHER TIPS

Why don't you simply pass the size to the constructor and initialize the array with it?

class Sudokus 
{
  char[] field;
  public Sudokus (int size){ field = new char[size*size]; }
}

The only advantage I could see with a "generic parameter" is if you had a fixed size array, which could allow for faster access, but that isn't supported in C# anyway (at least not as non-local variable).

I know this kind of templating is supported in C++, which templating is far superior to templating in C# or Java.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top