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?

1 Answer 1

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();
  }
}
1
  • 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 9, 2017 at 10:55

Your Answer

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

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