On Friday, 1 September 2017 at 23:25:04 UTC, Jesse Phillips wrote:
I've love being able to inherit and override generic functions in C#. Unfortunately C# doesn't use templates and I hit so many other issues where Generics just suck.

I don't think it is appropriate to dismiss the need for the compiler to generate a virtual function for every instantiated T, after all, the compiler can't know you have a finite known set of T unless you tell it.

But lets assume we've told the compiler that it is compiling all the source code and it does not need to compile for future linking.

First the compiler will need to make sure all virtual functions can be generated for the derived classes. In this case the compiler must note the template function and validate all derived classes include it. That was easy.

Next up each instantiation of the function needs a new v-table entry in all derived classes. Current compiler implementation will compile each module independently of each other; so this feature could be specified to work within the same module or new semantics can be written up of how the compiler modifies already compiled modules and those which reference the compiled modules (the object sizes would be changing due to the v-table modifications)

With those three simple changes to the language I think that this feature will work for every T.

Specifying that there will be no further linkage is the same as making T finite. T must be finite.

C# uses generics/IR/CLR so it can do things at run time that is effectively compile time for D.

By simply extending the grammar slightly in an intuitive way, we can get the explicit finite case, which is easy:

foo(T in [A,B,C])()

and possibly for your case

foo(T in <module>)() would work

or

foo(T in <program>)()

the `in` keyword makes sense here and is not used nor ambiguous, I believe.

Regardless of the implementation, the idea that we should throw the baby out with the bathwater is simply wrong. At least there are a few who get that. By looking in to it in a serious manner an event better solution might be found. Not looking at all results in no solutions and no progress.


Reply via email to