Reddit: SafeD - The Safe Subset of D
Charles D Hixson
charleshixsn at earthlink.net
Sun Mar 23 10:33:12 PDT 2008
Walter Bright wrote:
>
> http://reddit.com/r/programming/info/6d210/comments/
http://www.digitalmars.com/d/2.0/safed.html
A bit light on the syntax, but I completely agree with
avoiding pointers whenever possible. Unchecked casts and
unions are also clearly dangerous, if less so.
Unfortunately, unchecked casts seem to be necessary for some
purposes. It's not just efficiency, I literally don't see
any way around them. (I'm considering a union to be a kind of
unchecked cast.)
P.S.: What is a checked cast? If I want to consider a long
as an array of bytes:
byte[long.sizeof] val;
it ought to be safe to later consider that array of bytes as a
long. But what's a safe (checked) way to do it? (I'm not
talking about conversion. I don't want to end up with an
array of longs.)
PPS: When talking about casts or type conversions, please
make it explicit whether the same bit pattern is maintained.
I often read those descriptions, and realize that I can't
figure out exactly what is happening. With C I was always
certain that I was just telling the compiler to think about
the same piece of memory differently, and that nothing
actually changed. With more modern languages, a lot more
magic happens under the hood, and I'm no longer as certain
what's going on. I often wonder after reading the
documentation whether the same bit pattern is maintained, or
whether an equivalent value is produced. E.g., I've never
tried casting a float to a long. What would it produce? I
can't predict. I'd often prefer to deal with ulongs or ucents
rather than byte arrays, but then at other times I need to
address particular bytes out of that value. Because I don't
really understand a cast, I just use byte arrays (well,
ubyte). But it's "sloppier". Generally I'm dealing with a
unitary entity, and needing to think of it as an array all the
time is uncomfortable. (I'd even like a notation for dealing
with particular bits, though I haven't needed that recently.)
Note that this isn't a request for a change in how things act,
but rather in how they are documented.
I *suspect* that cast is presumed to be defined by C, and that
it means "Think about they type differently, but don't change
it's bit pattern", but I'm never quite certain.
More information about the Digitalmars-d-announce
mailing list