On 11/14/17 6:09 PM, H. S. Teoh wrote:
On Tue, Nov 14, 2017 at 11:05:51PM +0000, Michael V. Franklin via Digitalmars-d 
wrote:
On Tuesday, 14 November 2017 at 13:54:03 UTC, Steven Schveighoffer wrote:

IMO, no character types should implicitly convert from integer
types. In fact, character types shouldn't convert from ANYTHING
(even other character types). We have so many problems with this.

Is everyone in general agreement on this?  Can anyone think of a
compelling use case?
[...]

I am 100% for this change.  I've been bitten before by things like this:

        void myfunc(char ch) { ... }
        void myfunc(int i) { ... }

        char c;
        int i;

        myfunc(c);      // calls first overload
        myfunc('a');    // calls second overload (WAT)
        myfunc(i);      // calls second overload
        myfunc(1);      // calls second overload

I couldn't believe that this is the case so I tested it:

https://run.dlang.io/is/AHQYtA

for those who don't want to look, it does indeed call the first overload for a character literal, so this is not a problem (maybe you were thinking of something else?)

There is no compelling use case for implicitly converting char types to
int.  If you want to directly manipulate ASCII values / Unicode code
point values, a direct cast is warranted (clearer code intent).

I think you misunderstand the problem. It's fine for chars to promote to int, or even bools for that matter. It's the other way around that is problematic.

To put it another way, if you make this require a cast, you will have some angry coders :)

if(c >= '0' && c <= '9') value = c - '0';

Converting char to wchar (or dchar, or vice versa, etc.) implicitly is
also fraught with peril: if the char happens to be an upper byte of a
multibyte sequence, you *implicitly* get a garbage value.  Not useful at
all.  Needing to write an explicit cast will remind you to think twice,
which is a good thing.

Agree, these should require casts, since the resulting type is probably not what you want in all cases.

Where this continually comes up is char ranges. Other than actual char[] arrays, the following code doesn't do the right thing at all:

foreach(dchar d; charRange)

If we made it require a cast, this would find such problems easily.

-Steve

Reply via email to