3 ms·
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"
by nadinengland 10mo ago
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.
- charleszw 10mo agoYes, I constantly use this pattern in C++/JavaScript, although I haven't tested how performant it is in the former (what does the compiler even do with such an expression?)
- paavohtl 10mo agoAt least in simple cases the compiler will just inline the closure, as if it never existed. There shouldn't be any measurable overhead.
- dwattttt 10mo agoBy the by, code blocks on here are denoted by two leading spaces on each line like this
- nadinengland 10mo agoah, much appreciated!
- notpushkin 10mo agoInteresting that you can use blocks in JS: { const x = 5; x + 5 } // => 10 x // => undefined But I don’t see a way to get the result out of it. As soon as you try to use it in an expression, it will treat it as an object and fail to parse.
- pwdisswordfishy 10mo agohttps://github.com/tc39/proposal-do-expressions https://github.com/tc39/proposal-do-expressions (Not to be confused with do notation)