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