[chuck-users] Casting behaviour

Gonzalo gonzalo at dense13.com
Fri Oct 17 10:29:25 EDT 2014

I have this (unexpected?) behaviour when casting. I can cast a object to 
its parent class, but the methods that get executed are still their own 
(and viceversa). I.e.:

public class Parent {
   fun void test() {
     <<< "Parent" >>>;

class Child extends Parent {
   fun void test() {
     <<< "Child" >>>;

Parent p;
Child @ ch;
p $ Child @=> ch;
ch.test(); // prints Parent

Child ch2;
ch2 $ Parent @=> p;
p.test(); // prints Child

Is this intended behavior? What's the point of casting then?

I might describe too what I'm trying to do, maybe there's a better 
approach. I have a .create method in Parent that returns a Parent 
object, and I wanted to create that same method in Child, but I can't do 
it because the arguments are the same, and it won't let me overwrite the 
method when only the return type changes. So to create a child with the 
.create method, I do this:

Parent.create(...) $ Child @=> Child ch;

Which works, but then it still behaves as a parent, so it defeats the 
whole purpose.


More information about the chuck-users mailing list