States – Miscellaneous¶
Unowned references¶
When there may be an owner of an object, other references should not be used to modify the state. For example:
function foo(LightSwitch s) public { // s is unowned
s.turnOff(); // Shouldn't change state of s through an unowned reference
}
When unowned references are used in dynamic state tests, the body of the test does NOT have additional state information. For example:
function foo(LightSwitch s) { // s is unowned
if (s.state() == LightSwitch.State.On) {
s.turnOff(); // STILL can't change state of s through an unowned reference
}
}
If you need to call a function that needs the referenced object to be in a particular state, you need to start with at least a shared
reference (if not an owned
or state-specifying reference).
Implicit casts¶
When a shared
reference is needed, an owned
suffices as long as the reference is NOT to an asset. For example, an owned
reference can be passed as an argument to a function that expects a shared
reference to a non-resource object. However, the caller is left with a shared
reference.
When an unowned
reference is needed, any reference suffices, and the caller is left with their original kind of reference.