Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: generalized templates #14619

Closed
Opiumtm opened this issue Oct 19, 2016 · 5 comments
Closed

Discussion: generalized templates #14619

Opiumtm opened this issue Oct 19, 2016 · 5 comments

Comments

@Opiumtm
Copy link

Opiumtm commented Oct 19, 2016

Entire class of language limitations which cause boilerplate code and means to avoid it is a special case of one generic operation.

It's a class template and its specification.

template + template parameters -> concrete class

Generics are special case of this operation.
But if we see at most common OOP patterns they resemble same operation of some generic logic applied to concrete type arguments.

So, introducing of generalized class templates to C# would solve many cases of boilerplate code at once.

Let's formulate some requirements for it:

  1. Type-safety. Templates should be inspectable at compile time and template parameters should be validable at compile time.
  2. Should be embedded in result binary file and should be reusable by 3-rd party code in its primary form. So metadata should describe template entirely.
  3. Should be inspectable at run-time and concrete classes should be constructable from templates at run-time using same argument validation as at compile time. I.e. support for reflection.
  4. Should be simple to use and maintain. I.e. not being too low-level.

So some steps is required to do its work:

  1. There would be some parametrized custom C# code which would generate information for concrete type construction at run-time and at compile time.
  2. Runtime should be able to instantiate and validate for type-safety newly created classes.
  3. Concrete type created at compile-time could be plain CLR class with some additional metadata referencing template and template arguments. So, much of the work would be done in advance and wouldn't introduce footprint on this concrete type construction.
  4. Constructed at run-time concrete template implementations should be cached and be re-used when template construction is invoked with the same template arguments.

Another important thing to consider is a problem of early introduction and later usage of interfaces.
And more generic it's a problem of ability to put some object when well-known behavior (interface) is expected. Already defined type may meet those later expectations but it wasn't known at compile time when type was created.
It's why people are requesting structural typing and write boilerplate wrapper code.

This problem also could be solved by generalized template concept as adapter template could construct wrapper at run-time or at compile-time having source (any type) and target (interface) types as template parameters.

Static and dynamic code generation which would be considered as a special case of "template + template parameters -> concrete type" operation is already used by .NET CLR and .NET FX

Examples:

  1. Generics
  2. Regular expressions (RegularExpression can take regex string as template argument and generate concrete regex class at run-time)
  3. Real proxies
  4. XML transformations (XSLT)
  5. Serializers (.NET Native toolchain is generating optimized C# serialization code at compile-time for well-known serializers)
  6. .NET/COM interop wrappers
  7. Huge amount of custom code-generation tools and Visual Studio extensions often using T4 templates.

So application of this "template + template parameters -> concrete type" is very broad.
It's already used, but not in generalized nor standardized form.

@AdamSpeight2008
Copy link
Contributor

Also see #174 #8065

@Opiumtm
Copy link
Author

Opiumtm commented Oct 19, 2016

@AdamSpeight2008
One important thing to consider is an ability to invoke concrete class construction from template at the runtime, embedding class construction code and metadata into the result binary file - i.e. make this feature first-class citizen having reflection support.

@AdamSpeight2008
Copy link
Contributor

@Opiumtm It was more of making you aware of prior topics are this area, and the reluctance to implement templates / macro like language features into C# / VB.net.

@gulshan
Copy link

gulshan commented Jan 30, 2017

@CyrusNajmabadi
Copy link
Member

Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks!

@CyrusNajmabadi CyrusNajmabadi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants