www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10909] New: std.conv.to!(bool)(int): conversion from integer to bool

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

           Summary: std.conv.to!(bool)(int): conversion from integer to
                    bool
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: growlercab gmail.com


--- Comment #0 from growlercab gmail.com 2013-08-26 22:46:03 PDT ---
Improve std.conv.to!bool(int) to convert from integer to bool.

Compiling 0.to!bool gives the following compiler error:

Error: template std.conv.toImpl cannot deduce template function from argument
types !(bool)(int)

I would expected the following snippet to compile and throw no assertions...

---
import std.conv;

void main() {
    assert(0.to!bool == false);
    assert(1.to!bool == true);


    int ival = 1;
    assert(ival.to!bool == true);

    ival = 0;
    assert(ival.to!bool == false); 


    // Could follow C++ implicit conversion rules perhaps? 
    // Where non-zero == true
    ival = 55;
    assert(ival.to!bool == true);
}
---

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


monarchdodra gmail.com changed:

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


--- Comment #1 from monarchdodra gmail.com 2013-08-26 23:10:15 PDT ---
The current semantics of "to!X" means that there is range validation. This
means that something such as:

"to!bool(55)" *should* trigger an overflow exception.

This might sound inconvenient at first, but on the other hand, if it didn't,
than to would just be a glorified cast to bool.

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



--- Comment #2 from growlercab gmail.com 2013-08-26 23:31:00 PDT ---
(In reply to comment #1)
 The current semantics of "to!X" means that there is range validation. This
 means that something such as:
 
 "to!bool(55)" *should* trigger an overflow exception.
 
 This might sound inconvenient at first, but on the other hand, if it didn't,
 than to would just be a glorified cast to bool.
Good point, range checking should be in place so ignore that last part of the code. If this is implemented then the following should work: --- import std.conv; void main() { assert(0.to!bool == false); assert(1.to!bool == true); int ival = 1; assert(ival.to!bool == true); ival = 0; assert(ival.to!bool == false); } --- Thanks, G. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 26 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10909



--- Comment #3 from monarchdodra gmail.com 2013-08-27 00:13:46 PDT ---
The fix is pretty trivial, bools where not supported for the simple fact that
they are not on the "support" list.

The fix is:

1. std.conv:

Line 1309 Change:
T toImpl(T, S)(S value)
    if (!isImplicitlyConvertible!(S, T) &&
        (isNumeric!S || isSomeChar!S) &&
        (isNumeric!T || isSomeChar!T) && !is(T == enum))

To:
T toImpl(T, S)(S value)
    if (!isImplicitlyConvertible!(S, T) &&
        (isNumeric!S || isSomeChar!S || isBoolean!S) &&
        (isNumeric!T || isSomeChar!T || isBoolean!T) && !is(T == enum))

2. Traits:
Line 5691 Change:
template mostNegative(T)
    if(isNumeric!T || isSomeChar!T)

To:
template mostNegative(T)
    if(isNumeric!T || isSomeChar!T || isBoolean!T)

I don't have time to fix this myself right now, but if someone else does it,
and writes the corresponding unittests, I'd be glad to review it.

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



--- Comment #4 from growlercab gmail.com 2013-08-27 04:26:05 PDT ---
(In reply to comment #3)
 The fix is pretty trivial, bools where not supported for the simple fact that
 they are not on the "support" list.
 
 The fix is:
 
 1. std.conv:
 
 Line 1309 Change:
 T toImpl(T, S)(S value)
     if (!isImplicitlyConvertible!(S, T) &&
         (isNumeric!S || isSomeChar!S) &&
         (isNumeric!T || isSomeChar!T) && !is(T == enum))
 
 To:
 T toImpl(T, S)(S value)
     if (!isImplicitlyConvertible!(S, T) &&
         (isNumeric!S || isSomeChar!S || isBoolean!S) &&
         (isNumeric!T || isSomeChar!T || isBoolean!T) && !is(T == enum))
 
 2. Traits:
 Line 5691 Change:
 template mostNegative(T)
     if(isNumeric!T || isSomeChar!T)
 
 To:
 template mostNegative(T)
     if(isNumeric!T || isSomeChar!T || isBoolean!T)
 
 I don't have time to fix this myself right now, but if someone else does it,
 and writes the corresponding unittests, I'd be glad to review it.
https://github.com/D-Programming-Language/phobos/pull/1525 OK, I had a go at this. It is my first D contribution so hopefully I did everything correctly. Cheers, G -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10909



--- Comment #5 from growlercab gmail.com 2013-08-27 04:29:06 PDT ---
(In reply to comment #4)
 (In reply to comment #3)
 The fix is pretty trivial, bools where not supported for the simple fact that
 they are not on the "support" list.
 
 The fix is:
 
 1. std.conv:
 
 Line 1309 Change:
 T toImpl(T, S)(S value)
     if (!isImplicitlyConvertible!(S, T) &&
         (isNumeric!S || isSomeChar!S) &&
         (isNumeric!T || isSomeChar!T) && !is(T == enum))
 
 To:
 T toImpl(T, S)(S value)
     if (!isImplicitlyConvertible!(S, T) &&
         (isNumeric!S || isSomeChar!S || isBoolean!S) &&
         (isNumeric!T || isSomeChar!T || isBoolean!T) && !is(T == enum))
 
 2. Traits:
 Line 5691 Change:
 template mostNegative(T)
     if(isNumeric!T || isSomeChar!T)
 
 To:
 template mostNegative(T)
     if(isNumeric!T || isSomeChar!T || isBoolean!T)
 
 I don't have time to fix this myself right now, but if someone else does it,
 and writes the corresponding unittests, I'd be glad to review it.
https://github.com/D-Programming-Language/phobos/pull/1525 OK, I had a go at this. It is my first D contribution so hopefully I did everything correctly. Cheers, G
Thanks for the help monarch_dodra! -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10909



--- Comment #6 from github-bugzilla puremagic.com 2013-09-04 07:57:28 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/dcb634e8a27c9d732e3b30d56051ad1257f198a7
issue 10909 toImpl support for bool narrowing conversion. mostNegative support
for bool.

https://github.com/D-Programming-Language/phobos/commit/b001c0a7866887a0fcdc12a1e5780dfe0b2e9dd8
Merge pull request #1525 from lyrebirdsw/issue_10909

issue 10909 toImpl narrowing conversion support for bool type. mostNegative
support for bool type

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


hsteoh quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #7 from hsteoh quickfur.ath.cx 2013-09-04 08:12:30 PDT ---
Verified fixed in git HEAD (second version of code, in comment #2). The first
version still throws, as explained in comment #1.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 04 2013