Should you be able to initialize a float with a char?
Walter Bright
newshound2 at digitalmars.com
Thu May 19 04:10:04 UTC 2022
On 5/18/2022 5:57 PM, H. S. Teoh wrote:
> 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
I generally avoid using shorts. I agree the situation is hardly ideal, but there
is no ideal way I've ever seen. The various schemes just shift the deck chairs
around.
> 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.
The only reason to ever use shorts is to save memory in a frequently allocated
data structure. Short local variables do not save memory or time (in fact,
they're larger and slower). If you're doing all these casts, perhaps look into
using ints instead.
> 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.)
There's nothing wrong with:
if ('A' <= c && c <= 'Z')
c = c | 0x20;
D doesn't have C's problems with optionally signed chars, 10 bit chars, EBCDIC,
RADIX50 and other dead technologies.
More information about the Digitalmars-d
mailing list