Closed thekid closed 1 year ago
Can we emit using goto
? Strategy would be to:
return
with assignment and goto
// Example 2
foreach ($this->list as $element) {
if ($search === $element) { $found= true; goto __l0; }
}
{ $found= false; goto __l0; } // Maybe optimize this last line to omit the goto
__l0:
Unclear how this would work for example 1 though.
Still don't have a good idea, closing for now.
See also https://externals.io/message/120606 ("Expression code blocks"):
Sometimes it would be nice to have a code block inside an expression, like this:
public function f(string $key) { return $this->cache[$key] ??= { // Calculate a value for $key. [...] return $value; }; }
Before
After
Emitted as IIFE
Easiest to emit, orders of magnitude more expensive than
if/else
code before (first tests show a factor between 5 and 6)See https://babeljs.io/docs/en/babel-plugin-proposal-do-expressions
Note: For example 2, generator comprehensions could also work, see #59 and https://stackoverflow.com/questions/55004849/using-list-comprehension-for-finding-1-element-in-a-list