3

I want one contract to be able to copy fields from another.

contract Klendathu {
  string public bugs;

  function Klendathu(string _bugs) public {
    bugs = _bugs;
  }
}

contract RogerYoung {
  string public bugs;

  function getBugsFromKlendathu(Klendathu bigK) public {
    bugs = bigK.bugs();
  }
}

I get the error

TypeError: Type inaccessible dynamic type is not implicitly convertible to expected type string storage ref.
        bugs = bigK.bugs();
               ^---------^

How do I copy a string from one contract to another?

Dave Sag
  • 879
  • 1
  • 11
  • 22

1 Answers1

4

The reason behind this problem is that currently the EVM is unable to read variably-sized data from external function calls i.e. strings.

consider whether using bytes32[] instead of strings in this instance would be a suitable alternative - and offload the heavy lifting of converting from/to string for the UI to the middleware.


contract Klendathu {
  bytes32 public bugs;

  function Klendathu(bytes32 _bugs) public {
    bugs = _bugs;
  }
}

contract RogerYoung {
  bytes32 public bugs;

  function getBugsFromKlendathu(Klendathu bigK) public {
    bugs = bigK.bugs();
  }
}
SteveJaxon
  • 2,528
  • 1
  • 10
  • 21
  • Interesting. In one case of my actual code a bytes32 will suffice quite nicely. In the other I guess my only option is to use a dedicated contract to hold the string I need. It's a JOSE encrypted blob of data of uncertain length. That way it can be passed around as an address. – Dave Sag Nov 09 '17 at 10:55