Rust updates
Marco Leise
Marco.Leise at gmx.de
Wed Jul 11 12:55:04 PDT 2012
Am Wed, 11 Jul 2012 20:24:34 +0200
schrieb "bearophile" <bearophileHUGS at lycos.com>:
> Marco Leise:
>
> > Yes, this is the single most important Rust feature to me when
> > typing. I've just had too many cases of mass-casts to ubyte or
> > short where a suffix to the literal would only have cost one or
> > two letters. 255ub = byte, 32000s = short
>
> I am writing a lot of D2 code, but in my code the need of
> explicit short or byte literals is very uncommon. That's why
> those Rust suffixes seem overkill to me. Do you want to show us
> some of your use cases?
>
> Bye,
> bearophile
As an optional feature, I think it is nice to have them when you need them. They are not a feature that has any downside I can think of, as we already have some other suffixes and the maintenance cost is practically zero.
Here are some examples:
DMD:
(test42.d), array type inference:
const short[] ct = cast(short[]) [cast(byte)1, 1];
=> const short[] ct = cast(short[]) [1b, 1];
Phobos:
(boxer.d):
assert (box(1) == box(cast(byte)1));
=> assert (box(1) == box(1b));
(algorithm.d):
assert(b == [ cast(ubyte) 0, cast(ubyte)2, cast(ubyte)1, cast(ubyte)6, cast(ubyte)5], text(b));
=> assert(b == [ 0ub, 2ub, 1ub, 6ub, 5ub], text(b));
(variant.d):
a = cast(short) 5;
=> a = 5s;
(gregorian.d):
julianbegin = julianDay(tuple(cast(ushort)ymd._0, cast(ushort)1, cast(ushort)1))
=> julianbegin = julianDay(tuple(cast(ushort)ymd._0, 1us, 1us))
(string.d):
assert(isNumeric(cast(byte)0x57) == false); // 'W'
=> assert(isNumeric(0x57b) == false); // 'W'
GtkD:
(Color.d and several other occurences):
_black = new Color(cast(ubyte)0,cast(ubyte)0,cast(ubyte)0);
=> _black = new Color(0ub,0ub,0ub);
ScintillaD:
(SciTEWin.d):
SendMessage(HwndOf(wText), CB_SETEDITSEL, 0, MAKELPARAM(0, cast(ushort)-1));
=> SendMessage(HwndOf(wText), CB_SETEDITSEL, 0, MAKELPARAM(0, -1us));
QtD:
(Atomic.d):
return llvm_atomic_load_add!(ubyte)(cast(ubyte*)&val, cast(ubyte)0) ? 1 : 0;
=> return llvm_atomic_load_add!(ubyte)(cast(ubyte*)&val, 0ub) ? 1 : 0;
My code:
write a literal (byte) number to a 'take anything' output stream:
ostr.write(cast(ubyte) 0);
=> ostr.write(0ub);
from a unittest for Unicode sequences:
loadCycle(cast(char[]) [cast(ubyte) 0b11000111, cast(ubyte) 0b00111111]);
=> loadCycle(cast(char[]) [0b11000111ub, 0b00111111ub]);
I thought there were more cases in my code, but it seems like often the casts were required because some intermediate result of 16-bit calculations is extended to 32-bits by the language (where the CPU can perform arithmetics faster). So I looked at the other projects I checked out and it seems like casts to byte or short are not frequent, but most projects need them once in a while.
--
Marco
More information about the Digitalmars-d
mailing list