D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10909 - std.conv.to!(bool)(int): conversion from integer to bool
Summary: std.conv.to!(bool)(int): conversion from integer to bool
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2013-08-26 22:46 UTC by growlercab
Modified: 2013-09-04 08:12 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description growlercab 2013-08-26 22:46:03 UTC
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);
}
---
Comment 1 monarchdodra 2013-08-26 23:10:15 UTC
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.
Comment 2 growlercab 2013-08-26 23:31:00 UTC
(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.
Comment 3 monarchdodra 2013-08-27 00:13:46 UTC
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.
Comment 4 growlercab 2013-08-27 04:26:05 UTC
(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
Comment 5 growlercab 2013-08-27 04:29:06 UTC
(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!
Comment 6 github-bugzilla 2013-09-04 07:57:28 UTC
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
Comment 7 hsteoh 2013-09-04 08:12:30 UTC
Verified fixed in git HEAD (second version of code, in comment #2). The first version still throws, as explained in comment #1.