templates / generics
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 21
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.
Contributor guide
Research direction
Start by tracing the compiler's AST annotations and the annotation() function, then understand how the second type-checking phase transfers context through the AST. The work is complete when templated libraries, contracts, functions and user-defined types support the described syntax and are type-checked with their supplied expressions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, solidity
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 20/100