[chuck-users] Mixed Object Lists and Hierarchy
stefan.blixt at gmail.com
Sat Dec 12 07:20:21 EST 2009
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:
bars @=> Foo foos; // compile-time error, but let's say it works anyway
Spam spam; // extends Foo
spam @=> foos; // OK, a spam is a foo
<<< "Value: ", bars.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, 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
2009/12/11 Kassen <signal.automatique at gmail.com>
>> 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.
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
Release me, insect, or I will destroy the Cosmos!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the chuck-users