-
Notifications
You must be signed in to change notification settings - Fork 2.6k
ResultQuery/ custom Err type for Storage*::try_get #11010
Comments
but why? |
Reducing boilerplate. I personally find this: let thing = Self::try_get_thing(thing_id)?;
let thing2 = Self::try_get_thing2(thing2_id)?; Easier to grok than this: let thing1 = Thing::<T>::get(thing_id).ok_or(Error::<T>::ThingDoesNotExist);
let thing2 = Thing2::<T>::get(thing2_id).ok_or(Error::<T>::Thing2DoesNotExist)?; Especially when there are lots of storage accesses in a short period. As I mentioned in my first comment, this is quite a simple function, and currently I use this pattern extensively: fn get_thing(thing_id: &ThingId) -> Result<Thing, DispatchError> {
Thing::<T>::get(market_id).ok_or_else(|| Error::<T>::ThingDoesNotExist.into())
} I'm just wondering if there's a way to have this be auto generated. Maybe there could be a way to associate a storage with an |
I don't think number of characters saved is justifiable for extra macro magic especially with the help of auto completion. |
I think it actually makes sense. We could create a new |
That sounds like a really good idea! |
Fantastic idea, definitely feels more in line with the rest of the pallet macro!! How does something like this look? #[pallet::error]
pub enum Error<T> {
ThingNotFound,
...
} #[pallet::storage]
pub type Thing<T: Config> = StorageMap<
_,
Twox64Concat,
ThingId,
Thing,
ResultQuery<ThingNotFound>,
>; Where the ident provided to the Another option could be an attribute, a la |
In general, I dont like or use the However, |
I have noticed this common pattern throughout our codebase:
This can obviously be wrapped like this:
But this feels like unnecessary boilerplate for something this common.
My question is: is there a way to define a fallible getter for a storage?
Something like this, perhaps?
Where the 2nd argument is an arbitrary expression to return in
Err
if the value isn't found.If something else already exists that fits this use case/ issue, please let me know! I've looked around and couldn't find anything.
The text was updated successfully, but these errors were encountered: