-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ensure #469
Add ensure #469
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'm not a fan, for me this syntax is a bit unintuitive.
It should "sound" like: ensure that {condition}, otherwise {error}
, but because there's no "separation" between condition and returned error I need to spend this extra half a second to reverse condition and basically decode macro in head. :)
Long story short, looking at original syntax if {condition} then {error}
I read code faster then with proposed macro.
But leaving approve anyway. Code is clean, in some places shorter.
@@ -0,0 +1,34 @@ | |||
/// Quick check for a guard. If the condition (first argument) is false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a good place for this macro? If we'd commit to using it, basically every contract could use it so some packages/utils
seems more appropriate imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it belongs in cosmwasm_std. But once it is there it is hard to change.
Putting it here temporarily so we can experiment with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said, renaming cw0 to utils makes sense to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it. Maybe better called ensure_or
, but no need to change it.
I actually like Mauro's idea very much, it basically solves my issue and IMO sounds more Rusty. |
We could make I took |
Why? |
macro_rules! ensure_eq { | ||
($a:expr, $b:expr, $e:expr) => { | ||
if $a != $b { | ||
return Err($e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now what if we add
return Err($e); | |
return Err($e.into()); |
Wouldn't that open up a huge space of use cases in a very natural way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought this... but then it requires you to explicitly define the output. Like in the closures in the tests.
Maybe not being able to infer the type is worth it for the flexibility. 🤷
Can you make a PR on top. I am especially interested in the ergonomics of the change in real contract usages, not just my silly test code. It may be able to infer all types in the contracts, so my hesitation is unwarranted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do. This would make this macro behave like try!
and ?
when it comes to conversion where you have the same restriction on inference. See
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_deprecated(since = "1.39.0", reason = "use the `?` operator instead")]
#[doc(alias = "?")]
macro_rules! r#try {
($expr:expr $(,)?) => {
match $expr {
$crate::result::Result::Ok(val) => val,
$crate::result::Result::Err(err) => {
return $crate::result::Result::Err($crate::convert::From::from(err));
}
}
};
}
I agree. In fact I mentioned the same in planning today. |
Just rebased on main. Happy to either merge this with future PRs on top to improve, or others to PR on this to improve it. My main goal was to get something "good enough" into cw-plus utils, that we could iterate on. Either before 0.10.0 or after. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't get blocked by my suggestion. We can have a second look when migrating it to cosmwasm_std
I think the suggestion is good. If someone wants to try this in a new PR, go for it. |
Closes #468