recipes icon indicating copy to clipboard operation
recipes copied to clipboard

storage::maps with values wrapped in option

Open 4meta5 opened this issue 6 years ago • 0 comments

I've noticed that it's common for maps to initialize values as wrapped in an Option so that there is explicit handling of calls to storage in which the value does not exist. The calls to get the storage value often look like

if let Some(value) = <StorageValue<T>>::get(&key) {
  // some logic to use the value in the option
} else {
  // some logic to handle when the option is `None`
}

This could also be expressed as a match statement,

let v = <StorageValue<T>>::get(&key);
match v {
  Some(value) => {}, // some logic to use the value in the option
  None = {}, //some logic to use the value in the option
}

Or the most simple,

// inside a runtime method that returns a `support::dispatch::Result`
let v = <StorageValue<T>>::get(&key).ok_or("error message for None on call to storage")?;

4meta5 avatar Dec 04 '19 15:12 4meta5