Skip to content

Allow Immutable Strings in contracts #15983

Open
@Mouradif

Description

@Mouradif

Abstract

It's currently impossible to have immutable strings in a contract. This code fails:

contract MyToken {

  string immutable public name;

  constructor(string memory _name) {
    name = _name;
  }
}

with the compilation error

Error (6377): Immutable variables cannot have a non-value type.

But this code works:

contract MyToken {

  string constant public name = "Some Name";
}

In an ideal world, they would produce the exact same runtime bytecode (if the constructor argument of the first one was "Some Name").

Motivation

Most base ERC20 token implementations rely on storage for the name and symbol of the token because it's the only way for people to be able to extend their implementation and select a name for their own token. But in reality those could very well be constants (or immutable).

If Solidity allowed for immutable strings, then these implementations would use that instead and it would result in big gas savings especially for factories that deploy tokens (120k gas per token)

Specification

Ideally the syntax

string immutable someImmutable;

constructor (string memory x) {
  someImmutable = x;
}

would store the immutable someImmutable the same way as how constants are currently stored in the bytecode (and made accessible with a getter if the immutable is public)

Backwards Compatibility

This should be 100% backwards compatible. All contracts that previously compiled would still compile with the same bytecode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions