Should you be able to initialize a float with a char?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu May 19 00:57:44 UTC 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.


More information about the Digitalmars-d mailing list