xp-framework / compiler

Compiles future PHP to today's PHP.
19 stars 0 forks source link

Throw expressions #51

Closed thekid closed 6 years ago

thekid commented 6 years ago

In a nutshell

return $user ?? throw new IllegalStateException('No such user');

Would be allowed for the ternary operator, the null-coalesing operator and inside of compact functions.

See also

thekid commented 6 years ago

Could be realized by an IIFE:

// Given this input...
$a ?? throw new \lang\IllegalArgumentException("X");

// ...the compiler would emit this:
$a ?? (function() { throw new \lang\IllegalArgumentException("X"); })()
thekid commented 6 years ago

Good blog article on throws expressions in C# 7.0

http://gunnarpeipman.com/csharp/throw-expressions/

thekid commented 6 years ago

Stackoverflow question covering the problem:

https://stackoverflow.com/questions/9544976/using-throw-in-a-javascript-expression

thekid commented 6 years ago

In Kotlin, throw is an expression:

https://kotlinlang.org/docs/reference/exceptions.html#the-nothing-type