grep
Dee Girl
deegirl at noreply.com
Fri May 9 23:40:28 PDT 2008
torhu Wrote:
> janderson wrote:
> > I guess your not being serious but in case you are, you want to make bad
> > coding difficult to do and good coding easy. People area not likely to
> > go to that much effort to circumvent cast. There are a couple of other
> > ways to get around cast:
> >
> > //ie
> > class A
> > {
> >
> > };
> >
> > class B : A
> > {
> >
> > };
> >
> > B[] b;
> > b ~= new B;
> > A[] a = b;
> > a ~= new A;
> > //b[1] Is not an A type (not a B type), but is essentially a reinterpret
> > A -> B.
> >
>
> But array b has no element at index 1, it's length is still 1.
>
> This gets the desired effect:
>
> B[] b;
> b ~= new B;
> b ~= new B;
> A[] a = b[0..1];
> a ~= new A;
>
> Now, the second element of b will be an A. On the other hand, appending
> to slices could easily be considered bad style.
I do not understand D arrays yet to know why appending to slices is bad style. But there is no need for slices. The following program compiles and runs. D makes a textbook mistake by making arrays covariant. Why nobody gets arrays right?
import std.stdio;
class A {}
class B : A {}
void main()
{
B[] b = new B[2];
A[] a = b;
a[0] = new A;
a[1] = new B;
writeln(b[0]);
}
It is correct to allow B[] to be a subclass of const(A[]), but B[] can not be a subclass of A[] because elements of A[] can be written to and end up in the B[] array. Could D2 repair this bug? Dee Girl
More information about the Digitalmars-d
mailing list