Jeff Martin on Nostr: I just had a wild shower thought about #Rust / #RustLang : What if Rust had postfix ...
I just had a wild shower thought about #Rust / #RustLang :
What if Rust had postfix macros?
Something like:
let foo = bar().the_macro!();
where the postfix macro was able to transform its receiver somehow.
I often find myself writing boilerplate that looks something like:
let val = match foo() { Some(v) => v, None => { return; } };
But what if we could just do something like:
let val = foo().or! { return; };
I've always loved the Elvis operator in Kotlin that lets you do things like:
val foo = bar() ?: return
and I find myself reaching for something like that in Rust, but it's not there. Postfix macros might let you do something like that, and probably a whole lot more too. Maybe:
returns_a_result().warn_if_err!();
Something like the ? operator I guess, but more programmable.
What if Rust had postfix macros?
Something like:
let foo = bar().the_macro!();
where the postfix macro was able to transform its receiver somehow.
I often find myself writing boilerplate that looks something like:
let val = match foo() { Some(v) => v, None => { return; } };
But what if we could just do something like:
let val = foo().or! { return; };
I've always loved the Elvis operator in Kotlin that lets you do things like:
val foo = bar() ?: return
and I find myself reaching for something like that in Rust, but it's not there. Postfix macros might let you do something like that, and probably a whole lot more too. Maybe:
returns_a_result().warn_if_err!();
Something like the ? operator I guess, but more programmable.