2

I am trying to pass in a struct to the constructor of a contract but am getting the error

TypeError: Internal or recursive type is not allowed for public or external functions

The contract is

pragma solidity ^0.4.0;

contract Writer {

  struct Paragraph {
    string[] lines;
  }

  struct Essay {
    Paragraph[] paragraphs;
  }

  Essay[] private essays;

  function Writer(Essay initialEssay) {
    essays.push(initialEssay);
  }

}

I did some digging and it seems the issue is not actually to do with the struct being defined within the contract (where else would it be defined after all) but with the nested array, so I changed it to

contract Writer {

  struct Paragraph {
    string sentances;
  }

  struct Essay {
    string title;
    Paragraph[] paragraphs;
  }

  Essay[] private essays;

  function Writer(Essay essay) {
    essays.push(essay);
  }

}

but now I get

InternalCompilerError: Static memory load of more than 32 bytes requested.

So then I tried this way, manually constructing the struct.

contract Writer {

  struct Paragraph {
    string sentances;
  }

  struct Essay {
    string title;
    Paragraph[] paragraphs;
  }

  Essay[] private essays;

  function Writer(string title, string[] _paras) {
    Paragraph[] storage paras;
    for (uint256 i = 0; i < _paras.length; i++) {
        Paragraph memory para = Paragraph(_paras[i]);
        paras.push(para);
    }
    Essay memory initialEssay = Essay(title, paras);
    essays.push(initialEssay);
  }
}

which gives

UnimplementedFeatureError: Nested arrays not yet implemented.

Should I perhaps ditch using a struct and instead define a new contract to represent an Essay?

3

You can't pass a nested array as a function parameter if that function is external. When you are trying to pass _paras it will fail because string[] is a nested array.

The simplest solution for your problem, if it is not a lot of parameters is to just pass them each one as a separate parameter, construct the struct inside the function and push it to the array.

It would be like the last implementation you tried but instead pos passing string[] params you would pass string param1, string param2, string param3, etc.

  • Okay thanks. That’s kind of what I was afraid of. Alas the incoming data structure (as currently being proposed) is quite complex. Back to the drawing board with that one I think. – Dave Sag Oct 25 '17 at 13:03
  • Is this still valid? It's not possible to pass, e.g. a string[] as a parameter? – Senju Oct 16 '18 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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