[chuck-users] Mixed Object Lists and Hierarchy

mike clemow gelfmuse at gmail.com
Sat Dec 12 13:30:28 EST 2009

Hi there,

2009/12/12 Stefan Blixt <stefan.blixt at gmail.com>:
> Hmm, you shouldn't have to cast from a subclass to its superclass, should
> you?

Well, this is my feeling as well.  A foo is-a spam.  The object
reference should work fine this way...

spam spam;
spam @=> Foo foo;

Which, in fact, Chuck doesn't complain about.  Lists are confusing, however...

[ spam, spam1 ] @=> Foo foos[];

Incompatible types, however, confusingly...

Spam spam, spam1;
Foo foos[0];
foos << spam;
foos << spam1;

Works just fine!  I have no idea why this should be the case.


 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 <signal.automatique at gmail.com>
>> 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 at lists.cs.princeton.edu
>> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
> --
> Release me, insect, or I will destroy the Cosmos!
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users


More information about the chuck-users mailing list