Skip to content

templates / generics #869

Open
Open
@chriseth

Description

@chriseth

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    epic effortMulti-stage task that may require coordination between team members across multiple PRs.genericshigh impactChanges are very prominent and affect users or the project in a major way.language design :rage4:Any changes to the language, e.g. new featuresneeds designThe proposal is too vague to be implemented right awayselected for developmentIt's on our short-term development

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions