Implicit encoding conversion on string ~= int ?

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 23 22:39:46 PDT 2013


On Monday, June 24, 2013 07:20:10 Marco Leise wrote:
> Am Sun, 23 Jun 2013 17:50:01 -0700
> 
> schrieb Jonathan M Davis <jmdavisProg at gmx.com>:
> > I don't think that we even succeeded at coming close to
> > convincing Walter that _bool_ isn't an integral type and shouldn't be
> > treated as such (when it was discussed right before deconf), and that
> > should be a far more clearcut case.
> > 
> > - Jonathan M Davis
> 
> You can take bool to int promotion out of my...
> 
> // best way to toggle forth and back between 0 and 1. "!" returns a bool.
> value = !value
> 
> // don't ask, I've seen this :)
> arr[someBool]
> 
> // sometimes the bool has just the value you need
> length -= boolRemoveTerminator

And in all those cases, you can cast to int to get the value you want. The 
case that brought up the big discussion on it a couple of months ago was when 
you had

auto foo(bool b) {...}
auto foo(long l) {...}

Which one does foo(1) call? It calls the bool version, because of how the 
integer conversion rules work. IMHO, this is _very_ broken, but Walter's 
response is that the solution is to add the overload

auto foo(int i) {...}

And that does fix the code in question, but it means that bool is _not_ 
strongly typed in D, and you get a variety of weird cases that cause bugs 
because of such implicit conversions. I would strongly argue that the case 
where you want bool to act like an integer is by far the rarer case and that 
casting fixes that problem nicely. Plenty of others agree with me. But no one 
has been able to convince Walter.

You can read the thread here:

http://forum.dlang.org/post/klc5r7$3c4$1@digitalmars.com

- Jonathan M Davis


More information about the Digitalmars-d mailing list