Jump to page: 1 28  
Page
Thread overview
Should you be able to initialize a float with a char?
May 18, 2022
max haughton
May 18, 2022
Paul Backus
May 20, 2022
forkit
May 20, 2022
forkit
May 18, 2022
H. S. Teoh
May 19, 2022
Walter Bright
May 19, 2022
max haughton
May 19, 2022
Walter Bright
May 19, 2022
ab
May 19, 2022
Walter Bright
May 19, 2022
bauss
May 19, 2022
Walter Bright
May 19, 2022
H. S. Teoh
May 19, 2022
Walter Bright
May 19, 2022
bauss
May 19, 2022
Walter Bright
May 19, 2022
kdevel
May 19, 2022
Ali Çehreli
May 19, 2022
kdevel
May 19, 2022
deadalnix
May 19, 2022
kdevel
May 19, 2022
deadalnix
May 20, 2022
kdevel
May 20, 2022
Ali Çehreli
May 20, 2022
Walter Bright
May 19, 2022
deadalnix
May 20, 2022
Ali Çehreli
May 19, 2022
Walter Bright
May 19, 2022
deadalnix
May 20, 2022
Walter Bright
May 20, 2022
max haughton
May 20, 2022
Patrick Schluter
May 20, 2022
claptrap
May 20, 2022
Nicholas Wilson
[OT] Re: Should you be able to initialize a float with a char?
May 20, 2022
Nick Treleaven
May 19, 2022
user1234
May 19, 2022
Walter Bright
May 19, 2022
max haughton
May 19, 2022
user1234
May 19, 2022
Walter Bright
May 19, 2022
John Colvin
May 19, 2022
Walter Bright
May 19, 2022
deadalnix
May 19, 2022
max haughton
May 20, 2022
Walter Bright
May 20, 2022
deadalnix
May 20, 2022
Walter Bright
May 20, 2022
claptrap
May 20, 2022
deadalnix
May 20, 2022
Paul Backus
May 20, 2022
deadalnix
May 20, 2022
Paul Backus
May 20, 2022
forkit
May 20, 2022
forkit
May 19, 2022
deadalnix
May 19, 2022
H. S. Teoh
May 19, 2022
Walter Bright
May 19, 2022
max haughton
May 20, 2022
Walter Bright
May 20, 2022
max haughton
May 20, 2022
deadalnix
May 19, 2022
deadalnix
May 19, 2022
matheus
May 19, 2022
deadalnix
May 19, 2022
zjh
May 20, 2022
Ali Çehreli
May 18, 2022

For example:

float x = 'a';

Currently compiles. I had no idea that it does but I was implementing this pattern in SDC and lo and behold it does (and thus sdc has to support it).

Should it? Implicit conversions and implicit-anything around floats seem to be very undocumented in the specification too.

May 18, 2022

On Wednesday, 18 May 2022 at 22:11:34 UTC, max haughton wrote:

>

For example:

float x = 'a';

Currently compiles. I had no idea that it does but I was implementing this pattern in SDC and lo and behold it does (and thus sdc has to support it).

Should it? Implicit conversions and implicit-anything around floats seem to be very undocumented in the specification too.

Under "integer promotions", the spec says that char can implicitly convert to int. Under "usual arithmetic conversions", the spec says (by implication) that all arithmetic types can implicitly convert to float. "Arithmetic type" is not explicitly defined by the spec, but in the C99 standard it means "integer and floating types." It's probably safe to assume the same definition applies to D.

So I would say that according to the spec, the answer is "yes, the example should work." Though it is rather surprising.

May 18, 2022
On Wed, May 18, 2022 at 10:11:34PM +0000, max haughton via Digitalmars-d wrote:
> For example:
> 
> float x = 'a';
> 
> Currently compiles. I had no idea that it does but I was implementing this pattern in SDC and lo and behold it does (and thus sdc has to support it).
> 
> Should it? Implicit conversions and implicit-anything around floats seem to be very undocumented in the specification too.

If you were to ask me, I'd say prohibit implicit conversions between char and non-char types. Otherwise you end up with nonsense code like the above.

But IIRC, the last time this conversation came up, Walter's view was that they are all integral types and therefore should be interconvertible.  The topic at the time was bool vs int, but the same principle holds in this case.


T

-- 
MSDOS = MicroSoft's Denial Of Service
May 18, 2022
On 5/18/2022 3:31 PM, H. S. Teoh wrote:
> If you were to ask me, I'd say prohibit implicit conversions between
> char and non-char types. Otherwise you end up with nonsense code like
> the above.
> 
> But IIRC, the last time this conversation came up, Walter's view was
> that they are all integral types and therefore should be
> interconvertible.  The topic at the time was bool vs int, but the same
> principle holds in this case.

People routinely manipulate chars as integer types, for example, in converting case. Making them not integer types means lots of casting will become necessary, and overall that's a step backwards.
May 18, 2022

On 5/18/22 6:31 PM, H. S. Teoh wrote:

>

On Wed, May 18, 2022 at 10:11:34PM +0000, max haughton via Digitalmars-d wrote:

>

For example:

float x = 'a';

Currently compiles. I had no idea that it does but I was implementing
this pattern in SDC and lo and behold it does (and thus sdc has to
support it).

Should it? Implicit conversions and implicit-anything around floats
seem to be very undocumented in the specification too.

If you were to ask me, I'd say prohibit implicit conversions between
char and non-char types. Otherwise you end up with nonsense code like
the above.

If you were to ask me, I'd say prohibit implicit conversions between char types and any other types, including other char types. converting char to dchar isn't correct.

But I have little hope for it, as Walter treats a boolean as an integer.

-Steve

May 18, 2022

On 5/18/22 8:27 PM, Walter Bright wrote:

>

On 5/18/2022 3:31 PM, H. S. Teoh wrote:

>

If you were to ask me, I'd say prohibit implicit conversions between
char and non-char types. Otherwise you end up with nonsense code like
the above.

But IIRC, the last time this conversation came up, Walter's view was
that they are all integral types and therefore should be
interconvertible.  The topic at the time was bool vs int, but the same
principle holds in this case.

People routinely manipulate chars as integer types, for example, in converting case. Making them not integer types means lots of casting will become necessary, and overall that's a step backwards.

Supporting addition on char types (even with char + int) is still possible without allowing implicit conversions.

-Steve

May 19, 2022
On Thursday, 19 May 2022 at 00:27:24 UTC, Walter Bright wrote:
> On 5/18/2022 3:31 PM, H. S. Teoh wrote:
>> If you were to ask me, I'd say prohibit implicit conversions between
>> char and non-char types. Otherwise you end up with nonsense code like
>> the above.
>> 
>> But IIRC, the last time this conversation came up, Walter's view was
>> that they are all integral types and therefore should be
>> interconvertible.  The topic at the time was bool vs int, but the same
>> principle holds in this case.
>
> People routinely manipulate chars as integer types, for example, in converting case. Making them not integer types means lots of casting will become necessary, and overall that's a step backwards.

People do indeed (I'd question whether it's routine in a good D program, I'd flag it in code review) manipulate characters as integers, but I think there's something to be said for forcing people to go char -> suitable integer -> char.

We have u/byte, largely for descriptive purposes already, personally I try to use them for calculation even if the byte's value is from a char.
May 18, 2022
On Wed, May 18, 2022 at 05:27:24PM -0700, Walter Bright via Digitalmars-d wrote:
> On 5/18/2022 3:31 PM, H. S. Teoh wrote:
> > If you were to ask me, I'd say prohibit implicit conversions between char and non-char types. Otherwise you end up with nonsense code like the above.
> > 
> > But IIRC, the last time this conversation came up, Walter's view was that they are all integral types and therefore should be interconvertible.  The topic at the time was bool vs int, but the same principle holds in this case.
> 
> People routinely manipulate chars as integer types, for example, in converting case. Making them not integer types means lots of casting will become necessary, and overall that's a step backwards.

How is that any different from the current situation where arithmetic involving short ints require casts all over the place?  Even things like this require a cast:

	short s = 123;
	//s = -s; // NG
	s = cast(short)-s; // required excess verbiage

It got so out of hand that I wrote nopromote.d, specifically to "poison" expressions involving short ints with a custom struct with overloaded ops that always truncate, just so I don't have to litter my code with casts in just about every expression involving short ints.


In the case of char + int arithmetic, my opinion is that usually people do *not* (or *should* not) do char arithmetic directly -- with Unicode, it makes much less sense than the bad ole days of ASCII. These days, you'd call one of the std.uni functions for proper case mapping instead of a slipshod hack job of adding or subtracting some magic constant (which is wrong in anything except ASCII anyway).  In today's day and age, strings are best treated as opaque data that are manipulated by properly-implemented string functions in the standard library.  Having a few extra char/int casts in std.uni isn't the end of the world.  It shouldn't usually be done in user code anyway.  (And having to write lots of casts may motivate people to actually use proper string manipulation functions instead of winging it themselves with wrong implementations involving char arithmetic.)


T

-- 
Heuristics are bug-ridden by definition. If they didn't have bugs, they'd be algorithms.
May 19, 2022

On Wednesday, 18 May 2022 at 22:11:34 UTC, max haughton wrote:

>

SDC .

We have four D compiler?

May 18, 2022
On 5/18/2022 5:55 PM, max haughton wrote:
> People do indeed (I'd question whether it's routine in a good D program, I'd flag it in code review) manipulate characters as integers, but I think there's something to be said for forcing people to go char -> suitable integer -> char.

Casts are a common source of bugs, not correctness. This is because it is forced override of the type system. If the types change due to refactoring, the cast may no longer be correct, but the programmer will have no way of knowing.
« First   ‹ Prev
1 2 3 4 5 6 7 8