June 24, 2013 Re: Implicit encoding conversion on string ~= int ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Monday, 24 June 2013 at 00:50:17 UTC, Jonathan M Davis wrote:
> On Sunday, June 23, 2013 19:25:41 Adam D. Ruppe wrote:
>> On Sunday, 23 June 2013 at 16:37:18 UTC, bearophile wrote:
>> > I didn't know that, is this already in Bugzilla?
>>
>> I don't know, but if it is, it is probably marked as won't fix
>> because I'm pretty sure this has come up before, but it is
>> actually by design because a char in C is considered an integral
>> type too.
>
> This is definitely by design. Walter is definitely in the camp that thinks that
> chars are integral types, so they follow all of the various integral
> conversion rules.
Well, chars are integral values... but are integral values char?
I mean, I only see the promotion making sense one way: Converting a char to an uint can make sense for range analysis, but what about the other way around?
Same with bool: I can see bool to int making sense, but int to bool not so much, which is why a cast is required (except in an if).
In C, int to char was important, since char is the "byte" type. But D has byte, so I don't see why we'd allow int to byte...
|
June 24, 2013 Re: Implicit encoding conversion on string ~= int ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | Am Mon, 24 Jun 2013 08:03:27 +0200 schrieb "monarch_dodra" <monarchdodra@gmail.com>: > Well, chars are integral values... but are integral values char? > > I mean, I only see the promotion making sense one way: Converting a char to an uint can make sense for range analysis, but what about the other way around? > > Same with bool: I can see bool to int making sense, but int to bool not so much, which is why a cast is required (except in an if). > > In C, int to char was important, since char is the "byte" type. But D has byte, so I don't see why we'd allow int to byte... +1. This is probably the best thinking. It would allow: value = !value and its ilk, but prevent foo(1) from going to a bool overload. But should that work?: char D = 'A' + 3; char E = 4 + 'A'; -- Marco |
June 24, 2013 Re: Implicit encoding conversion on string ~= int ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | On 06/24/2013 07:20 AM, Marco Leise wrote: > Am Sun, 23 Jun 2013 17:50:01 -0700 > schrieb Jonathan M Davis <jmdavisProg@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 > value^=1 > // don't ask, I've seen this :) > arr[someBool] > > // sometimes the bool has just the value you need > length -= boolRemoveTerminator > |
June 24, 2013 Re: Implicit encoding conversion on string ~= int ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | 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@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 |
June 25, 2013 Re: Implicit encoding conversion on string ~= int ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | On Monday, 24 June 2013 at 05:20:31 UTC, Marco Leise wrote: > // best way to toggle forth and back between 0 and 1. "!" returns a bool. > value = !value If you're switching between 0 and 1, chances are you should be using a bool in the first place. > // don't ask, I've seen this :) > arr[someBool] Ew. > // sometimes the bool has just the value you need > length -= boolRemoveTerminator Ew. I think it's a big plus that it stops this needless obfuscation. |
Copyright © 1999-2021 by the D Language Foundation