Hmm, you shouldn't have to cast from a subclass to its superclass, should
you? In this case I'd say that a Bar array extends a Foo array...
Maybe what's going on here is that ChucK determines that the array you try
to reference copy into moreFoos consists of solely Bar objects, and
therefore it is of the type Bar[]. Maybe ChucK has adopted Java's type chuck
system for generics, meaning you can't assign a bar array to a Foo array.
Why? Because then you would be able to do this:
Bar bars[2];
bars @=> Foo foos[]; // compile-time error, but let's say it works anyway
Spam spam; // extends Foo
spam @=> foos[0]; // OK, a spam is a foo
<<< "Value: ", bars[0].bar >>>; // there's a spam here, with no bar!
...which would make no one happy. In Java, for an array, this code will work
until spam @=> foos[0], where you'll get an ArrayStoreException. For a
generic type (Java generics are a kind of imitation of C++ templates), you'd
get a compile error on the second of my lines above.
BTW, it would be great to have some way of extracting class information from
an object in ChucK, similarly to the Class objects in Java. It would be
useful for debugging situations like this.
Also, how do you cast arrays? This gives me a syntax error:
[ bara, barb, barc ] $ Foo[]
/Stefan
2009/12/11 Kassen
mike;
Why can't I make an array of a parent type with objects of a single type, which is a child of the parent?
Because you forgot to cast. You can cast anything to a type that it extends or that extends it (this includes Object). As long as you keep your inheritance very clean and overload member functions carefully this works fine and shouldn't damage your sanity. At least not much. Most of the time.
Yours, Kas.
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
-- Release me, insect, or I will destroy the Cosmos!