www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1626] New: bool spec problem

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

           Summary: bool spec problem
           Product: D
           Version: 1.022
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: davidl 126.com


According to :
http://www.digitalmars.com/d/1.0/expression.html
NumericLiteral is right under Primary Expressions

As we all know 
int i=3;
if (i) {} 
above is valid D code.

And right in:
http://www.digitalmars.com/d/1.0/statement.html#IfStatement
IfCondition is either Expression,auto Identifier = Expression, or Declarator =
Expression
The situation here that "i" is an Expression. 

and in the IfStatement part spec says: "Expression is evaluated and must have a
type that can be converted to a boolean."

I *assume* boolean here means bool, and it should be bool.

So expression here can be *implicitly* convert to bool.

But:

bool b=3; fails as expected with message  "Error: cannot implicitly convert
expression (3) of type int to bool"

3 is NumericLiteral therefore Primary Expressions, i.e. 3 is an Expression.

And the statement I concluded tells "expression can be *implicitly* convert to
bool"

so should 3 be implicitly converted to bool?

I hope not. The error message is obviously designed by Walter.

So maybe something more should be done to the spec.


-- 
Oct 29 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1626


matti.niemenmaa+dbugzilla iki.fi changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
           Keywords|                            |spec
         Resolution|                            |INVALID




------- Comment #1 from matti.niemenmaa+dbugzilla iki.fi  2007-10-29 09:10
-------
I think it's clear, in that it says "must have a type that can be converted to
a boolean".

It doesn't say "must have a type that can be *implicitly* converted to a
boolean", because that's not the case.

The type needs to be convertible in that if you cast it to bool *explicitly*,
it works. C.f. the following:

bool b = cast(bool)3;


-- 
Oct 29 2007
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1626


davidl 126.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




------- Comment #2 from davidl 126.com  2007-10-29 20:16 -------
I think you miss the point
if( 3 ) 
{
}
works
And in the spec:
"Expression is evaluated and must have a type that can be converted to a
boolean. If it's true the ThenStatement is transferred to"

Notice: If it's *true* the ThenStatement is transferred to
So what's *true*? I would think the evaluated result of the specific
expression.
And what type can have value *true*? bool is the answer.

Therefore, the expression is not only a type that *can* be converted to a
boolean, but also a type already be converted to a boolean. So the convertion
here is implicitly.


-- 
Oct 29 2007
parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Tue, 30 Oct 2007 01:16:20 +0000 (UTC), d-bugmail puremagic.com wrote:

 http://d.puremagic.com/issues/show_bug.cgi?id=1626
 
 davidl 126.com changed:
 
            What    |Removed                     |Added
 ----------------------------------------------------------------------------
              Status|RESOLVED                    |REOPENED
          Resolution|INVALID                     |
 
 ------- Comment #2 from davidl 126.com  2007-10-29 20:16 -------
 I think you miss the point
 if( 3 ) 
 {
 }
 works
 And in the spec:
 "Expression is evaluated and must have a type that can be converted to a
 boolean. If it's true the ThenStatement is transferred to"
 
 Notice: If it's *true* the ThenStatement is transferred to
 So what's *true*? I would think the evaluated result of the specific
 expression.
 And what type can have value *true*? bool is the answer.
 
 Therefore, the expression is not only a type that *can* be converted to a
 boolean, but also a type already be converted to a boolean. So the convertion
 here is implicitly.

I think that the confusion comes about because the form... if (X) where X does not contain a comparison operand, is really shorthand for ... if (X != 0) so the expression is already a boolean so no conversion is needed. That is to say "if (3 != 0)" returns a boolean already so its not a case of '3' being converted to a boolean at all. To repeat, "if (X)" is equivalent to "if (X !=0)" and is not equivalent to "if (cast(bool)X)". -- Derek (skype: derek.j.parnell) Melbourne, Australia 30/10/2007 2:33:56 PM
Oct 29 2007
parent Matti Niemenmaa <see_signature for.real.address> writes:
Derek Parnell wrote:
 I think that the confusion comes about because the form...
 
   if (X)
 
 where X does not contain a comparison operand, is really shorthand for ...
 
   if (X != 0)
 
 so the expression is already a boolean so no conversion is needed. That is
 to say "if (3 != 0)" returns a boolean already so its not a case of '3'
 being converted to a boolean at all.
 
 To repeat, "if (X)" is equivalent to "if (X !=0)" and is not equivalent to
 "if (cast(bool)X)".

"if (X != 0)" is equivalent to "if (cast(bool)X)" for all numerical types. However, "if (cast(bool)X)" also works for objects, arrays, and the like, which can't be compared to 0. Thus I'd say it is equivalent to "if (cast(bool)X)", as "if (X)" works for all types (except structs), as does "if (cast(bool)X)", whereas "if (X != 0)" only works for int and floating point types. For objects and arrays, it's "if (X !is null)". If X is already a boolean, the test done is "X != 0", which is why "if (X)" works but "if (X == true)" doesn't, when X contains a value which is neither true nor false. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Oct 30 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1626





------- Comment #3 from matti.niemenmaa+dbugzilla iki.fi  2007-10-30 04:03
-------
(In reply to comment #2)
 I think you miss the point
 if( 3 ) 
 {
 }
 works
 And in the spec:
 "Expression is evaluated and must have a type that can be converted to a
 boolean. If it's true the ThenStatement is transferred to"
 
 Notice: If it's *true* the ThenStatement is transferred to
 So what's *true*? I would think the evaluated result of the specific
 expression.
 And what type can have value *true*? bool is the answer.

Read "it" as "cast(bool)Expression". So, "if cast(bool)Expression is true". So, of course it's bool.
 Therefore, the expression is not only a type that *can* be converted to a
 boolean, but also a type already be converted to a boolean. So the convertion
 here is implicitly.

Yes, it is a type which can be converted to a boolean, and yes, the conversion is done implicitly. But no, it doesn't have to be implicitly convertible. An explicit cast is implicitly inserted. I'll leave this open now, so somebody with access to the doc can make of this what they will, but I don't see the problem. --
Oct 30 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1626


Christian Kamm <kamm-removethis incasoftware.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |kamm-removethis incasoftwar
                   |                            |e.de




--- Comment #4 from Christian Kamm <kamm-removethis incasoftware.de> 
2009-07-02 07:38:15 PDT ---
Something like this could be added to Statements/If Statement to make the spec
describe DMD's behavior:

If the condition expression v that is passed to the if statement is not of type
bool, a different expression depending on v's type is evaluated instead:
 * integer, floating point, complex, pointer, function: v != 0
 * array: v.ptr != 0
 * associative array: cast(void*)v != 0
 * delegate: v.funcptr != 0
 * class: v !is null
 * struct: error

Note that for struct and class type conditions, it is not equivalent to
evaluating cast(bool)v.

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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |WONTFIX


--- Comment #5 from Walter Bright <bugzilla digitalmars.com> 2010-12-05
22:23:53 PST ---
I don't think there's a problem. "Can be converted" doesn't mean "implicitly
convertible". Implicitly convertible means something very specific, and that
term would have been used if it was meant.

Will mark as "wontfix".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 05 2010