5 ms·
People keep wanting monadic syntax (I'm one of them), but I believe the general suggestion is to do it via macros/syntax extensions. E.g. write something that d
by dbaupp 13y ago
People keep wanting monadic syntax (I'm one of them), but I believe the general suggestion is to do it via macros/syntax extensions. E.g. write something that desugars
do! {
bind x = foo();
let bar = baz(10);
ret x + bar
}
into the equivalent of Haskell's
do
x <- foo
let bar = baz 10
return $ x + bar
Rust currently lacks higher-kinded types, so it would be hard to write functions that are generic over monads but the above would at least give some local reprieve.
Also, note the `if_ok!` macro alluded to in the point about IO errors is giving the shortcircuiting behaviour of the Either monad (specific to Either, though).
(NB. the macro probably can't be called `do` just yet, but we may remove that as a keyword. Maybe.)
- Ygg2 13y agoWait, how don't macro need to take escaped sigils? Like range! takes range!('[' 0 , Inf ')' )? Is 'do' still a keyword? I remember it being removed, pretty much everywhere. I just assumed it was thrown away.
- lucian1900 13y ago> Wait, how don't macro need to take escaped sigils? These are proper (hygienic) syntax macros, much like Scheme's syntax-rules. C's "macros" only work at the token level. > Is 'do' still a keyword? I believe it is still a reserved keyword, but not used anymore.
- dbaupp 13y agoThey need to take balanced braces/parens/brackets (i.e. only escaped if they are possibly unbalanced), so that Rust source can still be parsed without having to run the macro expander concurrently. And, it looks like do is no longer a keyword at all, I must've missed that part of the do-removal patch! So it is possible to call that macro do.