Thread overview
static if AssignExpression
Feb 13, 2019
Luís Marques
Feb 13, 2019
H. S. Teoh
Feb 13, 2019
rikki cattermole
Feb 13, 2019
bauss
February 13, 2019
`static if` is defined in the D grammar as `static if ( AssignExpression )`.
What's the point of AssignExpression, instead of just a ConditionalExpression?
In what situation can you even use `static if(x = y)?`, `static if(x += y)?`, etc?
February 13, 2019
On Wed, Feb 13, 2019 at 04:45:39PM +0000, Luís Marques via Digitalmars-d wrote:
> `static if` is defined in the D grammar as `static if (
> AssignExpression )`.  What's the point of AssignExpression, instead of
> just a ConditionalExpression?  In what situation can you even use
> `static if(x = y)?`, `static if(x += y)?`, etc?

It's either an oversight, or was probably chosen just for consistency with the normal `if` condition (to make it simpler to implement in the compiler since we could just use the same code for parsing the condition).

Though, in an extreme case, one could potentially have a user-defined that overloads += to return bool at compile-time.  Probably an example of operator overload abuse, though.


T

-- 
I don't trust computers, I've spent too long programming to think that they can get anything right. -- James Miller
February 13, 2019
On Wednesday, 13 February 2019 at 16:45:39 UTC, Luís Marques wrote:
> `static if` is defined in the D grammar as `static if ( AssignExpression )`.
> What's the point of AssignExpression, instead of just a ConditionalExpression?
> In what situation can you even use `static if(x = y)?`, `static if(x += y)?`, etc?

I would agree with that it makes no sense that it's an AssignExpression.

It would've made more sense if the normal if statement was an AssignExpression, but even that isn't.

I don't believe there are any situations where you'd end up with an AssignExpression as the condition for a static if statement.

Either there is a magic reason for some compiler bug that it avoids OR it's a simple mistake.
February 14, 2019
On 14/02/2019 6:33 AM, H. S. Teoh wrote:
> On Wed, Feb 13, 2019 at 04:45:39PM +0000, Luís Marques via Digitalmars-d wrote:
>> `static if` is defined in the D grammar as `static if (
>> AssignExpression )`.  What's the point of AssignExpression, instead of
>> just a ConditionalExpression?  In what situation can you even use
>> `static if(x = y)?`, `static if(x += y)?`, etc?
> 
> It's either an oversight, or was probably chosen just for consistency
> with the normal `if` condition (to make it simpler to implement in the
> compiler since we could just use the same code for parsing the
> condition).
> 
> Though, in an extreme case, one could potentially have a user-defined
> that overloads += to return bool at compile-time.  Probably an example
> of operator overload abuse, though.

Ugh...

https://github.com/dlang/dmd/blob/master/src/dmd/parse.d#L2407