Re: [RFC][DISCUSSION] throw expression

From: Date: Thu, 26 Mar 2020 12:04:49 +0000
Subject: Re: [RFC][DISCUSSION] throw expression
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-109331@lists.php.net to get a copy of this message
On 22.03.20, 18:17, "Dan Ackroyd" <Danack@basereality.com> wrote: > $condition || throw new Exception('$condition must be truthy') > && $condition2 || throw new Exception('$condition2 must be truthy'); > > The "Deprecate left-associative ternary operator"* RFC made it so that > parentheses are required when ternary operators are nested in > complicated statements. > > Would a similar requirement for parentheses around complicated throw > expressions be a suitable solution to avoid people being surprised by > the behaviour? I've thought about this some more. There is a reasonable way to recognize this, namely to check if the expression after the throw keyword is of type ZEND_AST_OR or ZEND_AST_AND. The expression above will fail with this message (given that $condition is false): ``` Fatal error: Uncaught Error: Can only throw objects in ... ``` IMO this is clear enough that a message for this edge case is not necessary.

« previous php.internals (#109331) next »