www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - static if AssignExpression

reply =?UTF-8?B?THXDrXM=?= Marques <luis luismarques.eu> writes:
`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?
Feb 13
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Feb 13, 2019 at 04:45:39PM +0000, Lus 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
Feb 13
parent rikki cattermole <rikki cattermole.co.nz> writes:
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
Feb 13
prev sibling parent bauss <jj_1337 live.dk> writes:
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.
Feb 13