I love that this is part of the syntax. I typically use closures to do this in other languages, but the syntax is always so cumbersome. You get the "dog balls" that Douglas Crockford always called them: ``` const config = (() => { const raw_data = ... ... return compiled; })()' const result = config.whatever; // carry on return result; ``` Really wish block were expressions in more languages.
Rust's Block Pattern
31–40 of 121 posts
Re: Rust's Block Pattern
#32Re: Rust's Block Pattern
#33GCC adds similar syntax as an extension to C: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html It's used all throughout the Linux kernel and useful for macros.
I use that with with macros to return akins to std::expected, while maintaining the code in the happy-path like with exceptions.
Re: Rust's Block Pattern
#34Re: Rust's Block Pattern
#35I have one better: the try block pattern. https://doc.rust-lang.org/beta/unstable-book/language-featur...
Why does this need special syntax? Couldn't blocks do this if the expression returns a result in the end?
Re: Rust's Block Pattern
#36I have one better: the try block pattern. https://doc.rust-lang.org/beta/unstable-book/language-featur...
Why does this need special syntax? Couldn't blocks do this if the expression returns a result in the end?
Re: Rust's Block Pattern
#37You can also de-mut-ify a variable by simply shadowing it with an immutable version of itself: let mut data = foo(); data.mutate(); let data = data; May be preferable for short snippets where adding braces, the yielded expression, and indentation is more noise than it's worth.
Re: Rust's Block Pattern
#38Re: Rust's Block Pattern
#39Re: Rust's Block Pattern
#40This is also somewhat common in c++ with immediate-invoked lambdas