The need to allocate memory for string

Viewed 1118

I'm learning Solidity and I'm stuck on memory vs storage vs calldata. I'm reading the documentation and found thisenter link description here:

Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables

Yet with an example contract of:

contract ExampleContract {
  string public myText = "Hello, world!";

  function getMyText() public view returns (string) {
    return myText;
  }
}

I get an error telling me Data location must be "memory" or "calldata" for return parameter in function, but none was given..

Why is there a requirement for strings to have explicitly defined data allocation (e.g. in function params or returns)?

1 Answers

This article has the answer to my question.

"Both of these [bytes and string types] are dynamic array types, which means that they can store data of arbitrary size. Each element of a variable of type bytes is, unsurprisingly, a single byte. Each element of a variable of type string is a character of the string."

One step closer to understanding Solidity :)

Related