[Issue 2270] cast produces invalid arrays at runtime

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jul 11 04:26:12 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=2270



--- Comment #3 from Stewart Gordon <smjg at iname.com> 2011-07-11 04:21:01 PDT ---
I disagree.  The primary purpose of casting is simply to force an expression to
a specified type.  This cast may or may not be legal.  For example, you can't
cast between different class and struct types arbitrarily, or cast an array
reference directly to a long (although, on 32-bit systems, they are the same
size).

It's true that some casts are unsafe, such as casting away constancy.  But this
is something one might actually want to do for various reasons (such as
interfacing some foreign APIs), and the behaviour is documented (though it
would be better if the means were explicit).

On the other hand, I can't see anybody wanting what actually happens when you
try casting an object array to an interface array.  And it's an easy mistake to
make, either
- by someone who hasn't yet learned that they aren't compatible
- by forgetting whether something's a class or an interface
- when trying in vain to do generic programming stuff that will work with
either classes or interfaces

Moreover, unless I'm mistaken there's no indication in the spec that it's legal
but dangerous.

As such, it should either be illegal, or convert at runtime.  The drawback of
the latter is that it would have to reallocate in order to perform the
conversion, which would cause further confusion in that carray and iarray point
to different areas of memory, contrary to the usual behaviour of array casting.

But I suppose what could be done is to define

    iarray[] = carray;

to convert each element of carray on copy, so that it works.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list