I'm a bit new to the Solidity-thing and am asking myself the question what to do with functions that specify a certain (set of) return parameter(s) and then fail to compute/find the desired return value(s).
In Java etc, I'd return a
null at this point or throw an Exception. Yet apparently, returning
null is not an option in Solidity and Exceptions are only doable via
Assume we have a
struct and a
mapping defined as follows
struct MyStruct(int x); mapping (address => MyStruct) private;
and my function wants to return the data stored in the mapping for the requester's address - only if it is in the mapping. Now, in case this function would not have the requested data it would have to return an error or a null. In solidity, I see these two approaches to handle a case like this:
Create an empty struct object to return. Bad practice, I would say.
revert()to cancel further execution of the contract's code. Sounds reasonable as unnecessary Gas is not burnt. Anyhow, causes an error that needs to be caught and handled by the client logic.
Is there a more elegant way to do this?