www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10638] New: Assignment can't be used as a condition

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10638

           Summary: Assignment can't be used as a condition
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: freeslave93 gmail.com



Suppose code

void main()
{
    int i = 0;
    if (i = 1)
    {
        //......
    }
}

dmd compiler generates error: "assignment cannot be used as a condition,
perhaps == was meant?"

But next code passed successfully:

void main()
{
    if (int i = 1)
    {
        //......
    }
}

It's a bit odd that assignment can not be used as condition while declaration
can be.

It works fine if we replace first code snippet with this:

void main()
{
    int i = 0;
    if (cast(bool)(i = 1))
    {
        //......
    }
}

I guess assignment has no implicit cast to bool, it's weird too. If it's not
error, please, explain me the reasons of this restriction. 

Also dlang.org defines ifStatement as "if ( IfCondition ) ThenStatement", where
ifCondition can be Expression (hence AssignExpression too), but it seems it
does not work at practice.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 14 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10638


9999 <mailnew4ster gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mailnew4ster gmail.com



I believe the primary reason is to prevent bugs where == was meant and not =.
Usually, what you want to do is:

void main()
{
    int i = f();
    if (i == 1)
    {
        //......
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 14 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10638


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc





 I believe the primary reason is to prevent bugs where == was meant and not =.
 Usually, what you want to do is:
 
 void main()
 {
     int i = f();
     if (i == 1)
     {
         //......
     }
 }
Right. D is working as designed here, it helps avoid bugs.
 Also dlang.org defines ifStatement as "if ( IfCondition ) ThenStatement",
 where ifCondition can be Expression (hence AssignExpression too),
 but it seems it does not work at practice.
Then maybe that's what needs fixing. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 14 2013