7

I'm learning Solidity and I'm stuck on memory vs storage vs calldata. I'm reading the documentation and found this:

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)?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
mdmb
  • 4,833
  • 7
  • 42
  • 90

2 Answers2

5

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 :)

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
mdmb
  • 4,833
  • 7
  • 42
  • 90
2

This is a little bit tricky because Strings are considered Array datatype in solidity and the data location needs to be specified when parsed, to either memory or calldata.

BiBi
  • 21
  • 3