9

How can I check if an Contract variable is undefined in Solidity?

6

According to this

Memory-stored objects as local variables are correctly zero-initialised: Members of structs and elements of fixed-size arrays are recursively initialised, dynamic arrays are set to zero length. delete x assigns a new zero-initialised value to x.

There is no such undefined variable. All objects are zero-initalised.

3

By some Solidity coding conventions you add a boolean 'valid' element to any struct to indicate it is undefined (false is the initialized value).

Sometimes I see fields having zero reserved to mean undefined - e.g. treat 0 as an undefined address. You could also do that for a string, but string is often externally input so you have to make sure someone didn't send you a zero string.

I would advise using a field where zero is naturally meaningful like a separate bool or an existing address element in a struct.

  • 1
    This is not useful for a pure Contract object isn't it? – arodriguezdonaire Mar 31 '16 at 14:31
  • what do you mean by a "pure Contract object"? – Paul S Mar 31 '16 at 19:44

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.