template ideas, not a proposal (yet), open for criticism #4680
Unanswered
dylanrafael05
asked this question in
General
Replies: 2 comments 4 replies
-
This can't be support by current CLR, and requires very heavy modification. |
Beta Was this translation helpful? Give feedback.
2 replies
-
This is very similar to shapes proposal, except that member signatures are not inlined. See #164 |
Beta Was this translation helpful? Give feedback.
2 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.
-
Motivation
Allowing for easy-to-read, easy-to-create, and performant methods which use operators, indexers, or properties of an unknown type.
Core features
A template is either a class (see Templates on classes) or a function which has one or more "template parameters".
For example, here is a function with a template argument;
When calling Min, the template argument must be provided, and it must be of constant form. This means that a template argument must either be:
A call to Min, for example
Min<int>(0, 1)
will be treated as a call to some functionMin__int
, which the compiler generates during compilation, with the signatureint Min__int(int a, int b)
. The compiler will decide which types must be used to generate a new function based on usage in the given code (possible due to the constant restraint on arguments).This function
Min__int
will be equivalent to a function where all instances ofT
are replaced withint
. This means that, similar to a normal generic, constraining the type ofT
using awhere
statement permits access to specific functions and operations that only a subset of types have."defines" constraint
Although templates are not particularly useful in the above case, template arguments also have access to a new type of constraint,
defines
. The syntax for this is as follows:The
defines
constraint checks if the template typeT
contains the given field, property, function, indexer, constructor, or operator, and that this item is accessible in the current scope. Similar towhere T: ISomeInterface
, anything stated to be defined by some template argumentT
can be used within the code whereT
is defined. For example;Important things to note:
Properties, indexers, and constructors have special rules when used with
defines
.For properties:
Indexer accessors follow the same rules as property accessors
For indexers: (subject to change)
For constructors: (subject to change)
Typed template arguments
Template args may also be given a type, denoted with a type placed between
template
and the parameter name, provided the type can be represented as a constant value;Note: the above function is especially useful because the compiler would recognize
bit
as a constant value, and likely replace all functions withreturn (value of 1 << bit);
, or even better inline the function as a literal. This is because the compiler sees any call to Bitmask as it would any other non-generic function call, having broken the function up into many based on the values ofbit
provided.These "typed template arguments" may not be constrained using
where
Argument order
There can be an arbitrary number of template arguments for anything which uses them.
All template arguments must go before normal generic arguments, and template constraints cannot reference normal generics:
Templates on classes
Templates may also be applied to classes, where the same rules about pre-compilation and template argument order and specifics apply;
Further features
A possible extension of these templates would be the introduction of deducible template types, for example;
Typed template arguments cannot be deduced.
End
Any thoughts? :P
Beta Was this translation helpful? Give feedback.
All reactions