2

Is there any way I can give a list of values (array or mapping) to the constructor of a contract?

In the style of the following code:

contract myContract {

    string[] public myArray;

    function myContract(string[] myArray) {
        myArray = myArray;
    }
}

I currently get the message from the online compiler:

Error: Internal type is not allowed for public or external functions.

1 Answer 1

2

it works if you change to int,address (which doesn't have a dynamic size as the string)or bytesX (fixed byte array)

  string[] public myArray;

  function   myContract (int[] myArray)  {

  }

so to understand why there is 2 answers :

1-in the doc :

generic arrays of fixed and dynamic size are supported in calldata and storage with the following features: Index access, copying (from calldata to storage, inside storage, both including implicit type conversion), enlarging and shrinking and deleting. Not supported are memory-based arrays (i.e. usage in non-external functions or local variables)

2-

Solidity doesn't have a built-in way yet to deserialize string arrays. It can cheat when the contract is passing an array it created itself to itself, which is why it works with private functions. But if you want to take string arrays from the outside, you're going to have to handle the serialization and deserialization from a string (or bytes) yourself. (E.g., concatenating fixed-length items, or using null characters as separators, or using commas, etc.)

source: Is it impossible to use an array of strings as the argument to solidity function?

2
  • That makes sense, I assume this applies to arrays of addresses also? Commented Oct 24, 2016 at 19:08
  • 1
    No address doesn't have a dynamic size like strings or bytes
    – Badr Bellaj
    Commented Oct 24, 2016 at 19:12

Your Answer

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

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