0

Let's say I have a base struct, Base. I can set it up like this;

struct Base{Struct2 data}

Here Struct2 is another structure. But what if I want to create a Base struct where the data type value is another type struct, let's say Struct1?

I know I can do it by;

struct Base1{Struct1 data}

But that's not what I want. I want to create a Base structure with Struct1 as data. Is this possible? I guess not.

1 Answer 1

0

I'm really not 100% sure I understand the question but I'm starting to think that the "value" side of things is supposed to be an arbitrary struct and the precise layout of this member is not known in advance?

Short answer, No. You're defining new types from known types and the layout needs to be unambiguous.

I can think of a number of ways to get that effect working within the confines of Solidity. You'll have to normalize it, either by defining a finite set of valid structures, by setting up arbitrarily complex (but normalized) sets of key/value pairs, or by something else.

Hope it helps.

2
  • Yes, that is basically my question. I guess that a similar question is if structs are constants or not (in terms of size), just like you are allowed to create dynamic arrays, but also arrays with a fixed side. In example here a dynamic struct would be a struct which allows you to pass different kind of structs to variables and fixed structs would not be able to change any variable type. I guess it is the latter. Thanks!
    – JBrouwer
    Mar 12, 2018 at 23:22
  • They're static in the sense that they are types and unambiguously laid out, but they can contain dynamic members, e.g. uint[] myUints,myOtherStruct[] myOtherStructs, or even mapping(keyType=>valType) mapName; Storage footprints are little strange in that the blockchain usually doesn't actually grow until a slot is explicitly written to. Mappings, for example, are (by definition) massive namespaces but they don't cost until a value is written to a location. Mar 12, 2018 at 23:37

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.