Description
Solidity should support template types / generics for contracts, libraries, functions and structs.
The syntax should be as follows:
library listImpl[T] {
struct List { T[] elements }
function append(List storage self, T _e) {
self.elements.push(_e);
}
}
In general, at every point where a new library, contract, function or user-defined type is defined, you can suffix the name with [T1, T2, ...]
.
Inside the library, contract, function or user-defined type, the identifiers T1, T2, ...
are bound to whatever they will be used with later. This means that type checking will not be done on those at the point where the template is defined.
At the point where a templated name is used, you have to prefix the name with [...]
, where inside the square brackets, an arbitrary expression is allowed. This will cause the template itself to be type-checked again, replacing the identifiers T1, T2, ...
with the respective values.
On the implementation side, this means that the AST annotations now have to be context-sensitive. Every template variable will be assigned a compiler-global identifier. The annotation()
function will receive an argument where these identifiers receive actual expressions. This argument will be transferred downwards in the AST during the second type checking phase.