1

I want to deploy an nft contract and i want to enter URI in constructor:

string immutable i_uri;

constructor(string memory name, string memory symbol, string memory uri) ERC721(name,symbol)  {
    i_uri = uri;
}

But i got this error : Immutable variables cannot have a non-value type.

What is the problem?? i haven't same problem with uint,address and etc.

2 Answers 2

0

From the documentation:

Not all types for constants and immutables are implemented at this time. The only supported types are strings (only for constants) and value types.

not implemented, use constant instead.

-1

The issue is that constant/immutable variables have their values defined during compilation and deployment, therefore I don't see a way to change them programmatically. Instead you could use a private variable.

pragma solidity ^0.8.0;

contract Example {
    bytes private i_uri;

    constructor(string memory name, string memory symbol, string memory uri) {
        i_uri = abi.encodePacked(uri);
    }

    function getURI() public view returns (string memory) {
        return string(i_uri);
    }
}

NOTE: Imutable is outdated, better to use contstant (For solidity ^0.8.0)

3
  • I know constant but you can not define constant value in constructor, constructor must entered directly, i want to enter value programmatically.
    – Alireza
    Commented Feb 5, 2023 at 7:25
  • And also i test it and it doesn't work because of constant, so i changed constant to immutable but also i got same error like string.
    – Alireza
    Commented Feb 5, 2023 at 7:32
  • The issue is that the "constant" is defined during deployment, and cannot be changed later. Therefore I think in this case, it might be best to use "private" variable instead. (I will update my code to explain)
    – Sky
    Commented Feb 5, 2023 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.