Skip to content

Allow Immutable Strings in contracts #15983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Mouradif opened this issue Apr 6, 2025 · 1 comment
Open

Allow Immutable Strings in contracts #15983

Mouradif opened this issue Apr 6, 2025 · 1 comment
Labels

Comments

@Mouradif
Copy link

Mouradif commented Apr 6, 2025

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.

@nadtech-hub
Copy link

I could work on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants