redefinition of variable in subclass

483 views
Skip to first unread message

Sergio Veg

unread,
Mar 10, 2014, 6:04:27 PM3/10/14
to haxe...@googlegroups.com
I want to add a custom setter in a subclass to a variable defined in a superclass, is it possible to do without changing the superclass?

Example:
class A {
     var v:Int;
}

what I want to do:
class B extends A{
     override v(default, set):Int;

     private function set_v(V:Int) {
         trace('new v=$V');
         return v = V;
     }
}

apixline

unread,
Mar 11, 2014, 10:33:46 AM3/11/14
to haxe...@googlegroups.com
override v(default, set):Int;
You can't redefine a var of superclass in a subclass. 

But you can do something like :
class A {
var v(default, set):Int;
public function new () { }
private function set_v(V:Int) :Int {
trace('class A');
return v = V;
}
static function main () {
trace(new B().v = 3); // B 6
trace(new A().v = 3); // A 3
}
}
class B extends A {
public function new () { super(); } 
override  private function set_v(V:Int) :Int {
trace('class B');
return v = V*2;
}
}
If A has only var v:Int; I think you have to use a new var in B.
hope that helps,
jm

Sergio Veg

unread,
Mar 11, 2014, 11:46:31 AM3/11/14
to haxe...@googlegroups.com
Actually the parent class A is in the public library and I don't think that it's correct to change the lib just for my needs. But from the language point of view, are there any theoretical restrictions to override variables? (if variable remains of the same type, but just adds the definition of setter or getter)

clemos

unread,
Mar 11, 2014, 11:59:20 AM3/11/14
to haxe...@googlegroups.com
Hi,
This has already been discussed a lot, actually.
The restriction is because the compiler needs to know the variable access (either 'default' or 'get') at compile type because getter/setter resolution is done then.
(Because native getter/setter support is not available / consistent across platforms)

If what you want was allowed, for instance this code :
var b : A = new B();
b.v = 1;
... which is perfectly valid, would generated this (for example in javascript):
var b = new B();
b.v = 1;
... instead of this :
var b = new B();
b.set_v(1);
... because at compile time, Haxe considers b of type A, and 'v' an actual variable, 
instead of considering b of type B, with 'v' being a getter variable.

As much as I'd love to be able to do it as well,
I don't think it's something trivial/easy to implement.

Best,
Clément


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

apixline

unread,
Mar 11, 2014, 1:11:21 PM3/11/14
to haxe...@googlegroups.com
>But from the language point of view, are there any theoretical restrictions to override variables? 
yes as Clemos explains ...

That is ok if it's you want to do (??) :
class A {
var v:Int;
}
class Main {
static function main () {
var b = new B(); b.aliasv = 3; trace (b.aliasv ); 
}
}
class B extends A {
public var aliasv(get,set):Int;
public function new () {  } 
private function set_aliasv(val:Int) :Int {
v = val * 2; return v;
}
private function get_aliasv():Int {
return v ;
}
}
Reply all
Reply to author
Forward
0 new messages