[Issue 2412] Hole in type system: array conversion to supertypes

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 10 06:27:55 PDT 2008


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


schveiguy at yahoo.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com




------- Comment #3 from schveiguy at yahoo.com  2008-10-10 08:27 -------
I'd agree with this.  It is inconsistent with how a pointer to a class is
treated, which is essentially what an array of classes references is.

i.e.:

X x = new x;
A* a = &x; // fails to compile, requires cast.

Similarly, if I look at an array, it is a struct:

struct ArrayOfX
{
  X *ptr;
  uint length;
}

Casting this to:

struct ArrayOfA
{
  A *ptr;
  uint length;
}

Seems like you would theoretically have to go through the same cast as the code
I wrote above, casting an X* to an A*.

The only drawback of this is you cannot return covariant arrays, or allow a
subtype array to be used as the base type for a function call.  I would guess
this is a rare requirement.

One thing that should probably be allowed though in D2 is implicitly casting to
a const(A)[].  This would prevent such mistakes as you have outlined.  You can
still explicitly cast if needed.


-- 



More information about the Digitalmars-d-bugs mailing list