Values of reference type can be modified through multiple different
names. Contrast this with value types where you get an independent
copy whenever a variable of value type is used. Because of that,
reference types have to be handled more carefully than value types.
Currently, reference types comprise structs, arrays and mappings. If
you use a reference type, you always have to explicitly provide the
data area where the type is stored: memory (whose lifetime is limited
to an external function call), storage (the location where the state
variables are stored, where the lifetime is limited to the lifetime of
a contract) or calldata (special data location that contains the
function arguments).
Warning
Prior to version 0.5.0 the data location could be omitted, and would default to different locations depending on the kind of variable, function type, etc., but all complex types must now give an explicit data location.
https://docs.soliditylang.org/en/latest/types.html#reference-types
so you have to put memory
or calldata
after String as follows:
contract MyContract {
string value;
function get() public view returns (string memory) {
return value;
}
function set(string memory _value) public {
value = _value;
}
constructor() {
value = "myValue";
}
}
another thing to notice that you dont have to put public in the constructor any more:
Warning: Prior to version 0.7.0, you had to specify the visibility of
constructors as either internal or public.
https://docs.soliditylang.org/en/latest/contracts.html?highlight=constructor#constructors
memory
andcalldata
and there use cases? – Gatto