Bug 10909 – std.conv.to!(bool)(int): conversion from integer to bool

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-08-26T22:46:00Z
Last change time
2013-09-04T08:12:30Z
Keywords
pull
Assigned to
nobody
Creator
growlercab

Comments

Comment #0 by growlercab — 2013-08-26T22:46:03Z
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 by monarchdodra — 2013-08-26T23:10:15Z
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 by growlercab — 2013-08-26T23:31:00Z
(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 by monarchdodra — 2013-08-27T00:13:46Z
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 by growlercab — 2013-08-27T04:26:05Z
(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 by growlercab — 2013-08-27T04:29:06Z
(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 by github-bugzilla — 2013-09-04T07:57:28Z
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 by hsteoh — 2013-09-04T08:12:30Z
Verified fixed in git HEAD (second version of code, in comment #2). The first version still throws, as explained in comment #1.