--- Comment #8 from 2012-01-20 17:45:30 PST ---
(In reply to comment #7)
> (In reply to comment #6)
> > The issue is the same for structs and any programmer who performs the cast 
> > is
> > aware of it. (otherwise they wouldn't use a cast ;))
> I'm am sure there are many programmers that are *not* aware of endianness, 
> even
> if they know that everything is made up of bytes and may use that cast.

If they are not aware of all relevant issues, they may not use a type cast.
Knowing what one does is a precondition for using a type cast.

> I was unaware of the consistency though. Personally I consider the ability to
> cast a struct to a ubyte[n] an error in the language design also.

If you didn't notice it existed, is it important enough to be called an

> Consider:
> ubyte[8] a = cast(ubyte[8]) iota(0, 8);
> writeln(a);
> You get [0, 0, 0, 0, 8, 0, 0, 0]
> I think this is something that an inexperienced D programmer could write
> expecting to get [0, 1, 2, 3, 4, 5, 6, 7] back.

This is a constructed example. If at all, they'll cast to ubyte[] (and that
fails). But inexperienced D programmers don't use type casts. It is the first
thing they learn about type casts. If they do, it is their own fault.

> Furthermore, you cannot rely on cast(ubyte[N]) to return a reinterpreted 
> struct
> because the struct may define opCast for ubyte[N] (imagine a container struct
> Array(T, size_t N) that has opCast for T[N] -- casting to ubyte[Array(T,
> N).sizeof] will reinterpret in most cases, except when T=ubyte and N=the
> sizeof, good luck finding that bug in your generic serialisation code).

Wrong. In most cases it will be a compiler error, because the compiler does not
fall back to reinterpreting if the struct defines an opCast. opCast is an
all-or-nothing thing.

> Reinterpreting memory should require nasty pointer casts. It's not common (or
> safe) enough to have convenient syntax, in my opinion.

Its only the pointer casts that are unsafe. cast(ubyte[4])1234 is perfectly
@safe. It will even catch size mismatches! (<insert 'good luck finding that
bug' comment here>)

By the way, it is possible to cast between two arbitrary structs of identical
size ;).

I would not mind if the feature was removed for structs. I'd just like to
restore consistency.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to