Hi list, Why can't I make an array of a parent type with objects of a single type, which is a child of the parent? class Foo { int foo; } class Bar extends Foo { int bar; } class Spam extends Foo { int spam; } Bar bara, barb, barc; Spam spama, spamb, spamc; Foo fooa, foob, fooc; [ bara, barb, barc ] @=> Bar bars[]; // okay [ bara, spamb, fooc ] @=> Foo foos[]; // mixed children okay [ bara, barb, barc ] @=> Foo morefoos[]; // incompatible types for assignment? best, Mike -- http://michaelclemow.com http://semiotech.org
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.
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!
Hi there,
2009/12/12 Stefan Blixt
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. Mike 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!
_______________________________________________ chuck-users mailing list chuck-users@lists.cs.princeton.edu https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
2009/12/12 mike clemow
Hi there,
2009/12/12 Stefan Blixt
: 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...
I agree. Back when I was starting out when classes and so on I did feel that this functionality was actually implied by the docs. I wonder whether that was intentional and that's actually meant to be how it should eventually work. This is one area where I do feel that the current docs need work. For one thing the "$" operator is only documented in how it casts from float to int, which is in and of itself a exception in that it loses information, unlike other forms of casting that need not lose information. Kas.
You can look at casting as something you use to help the compiler when
assigning values to variables that aren't obviously fit to contain them
(like an int for a float variable of a Foo for a Bar variable) - that's
what's common with copying floats and object references. For learning more
about it you can look it up in a Java or C manual, though they'll contain a
bit more than what's needed in ChucK (which doesn't have a lot of primitive
types for starters). I'm a Java fan, so I'd go here:
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5
/Stefan
2009/12/12 Kassen
2009/12/12 mike clemow
Hi there,
2009/12/12 Stefan Blixt
: 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...
I agree. Back when I was starting out when classes and so on I did feel that this functionality was actually implied by the docs. I wonder whether that was intentional and that's actually meant to be how it should eventually work. This is one area where I do feel that the current docs need work. For one thing the "$" operator is only documented in how it casts from float to int, which is in and of itself a exception in that it loses information, unlike other forms of casting that need not lose information.
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!
participants (3)
-
Kassen
-
mike clemow
-
Stefan Blixt