[chuck-users] Inheritance and Arrays

Julien Saint-Martin julien.saintmartin at googlemail.com
Fri Jan 2 10:15:54 EST 2015


Hi!

I would like to pass arrays of objects to a function taking an array of 
parent class as argument.
I try different things but unfortunately I have compilation errors each 
time.

The following example works with no array:

class A {
         int a;
}

class B extends A {
int b;
}

fun void plus(A arg) {
         arg.a ++;
}

B foo;

1 => foo.a;
plus (foo);
<<<"foo.a", foo.a>>>;




But When I try the same with arrays it doesn't compile:
[l13.ck]:line(22): argument type(s) do not match:
[l13.ck]:line(22): ... for function 'plus(...)' ...
[l13.ck]:line(22): ...(please check the argument types)

class A {
         int a;
}

class B extends A {
int b;
}

fun void plus(A arg[]) {
         for (0 => int i; i <  arg.size()     ; i++) {
         arg[i].a ++;
         }

}

B foo[2];

1 => foo[0].a;
2 => foo[1].a;
plus (foo);
<<<"foo[0].a", foo[0].a>>>;
<<<"foo[1].a", foo[1].a>>>;


Any idea ?

Thanks in advance and Happy new year!!

Julien



More information about the chuck-users mailing list