Overriding setters

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Overriding setters

Russell Weir
Curious.

Translating some source here, and I realize there's no actual way (that I know of) to call the super setter.

AS3
override public function set x(value:Number):void
{
     super.x = Math.round(value);
}

HAXE
class Blah extends Sprite {
@:setter(x) function setX(value : Float) {
        super.x = Math.round(value); // compiler error
        Reflect.setField(this, "x", Math.round(value)); // infinite loop
        x = /// don't even have to finish this one. infinite loop
}
}

Any way to do this? Some ultra secret Macro? Do I need to slap it with a fish?

R

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Overriding setters

rammserker
Only trough composition. For example:

var x (default,setX) : Float;
var compoSprite = new Sprite();

public function setX( inX : Float ) : Float {
    return x = compoSprite.x = inX;
}

That's the way I'm using it in translating a AS3 library to haXe. Also, I'm experimenting the way to make the composition trough Macro and it's kinda working, but I'm not sure if it's the best way of do it...

Max
0 1 0 | 0 0 1 | 1 1 1



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Overriding setters

Russell Weir
Oh, I know all about composition, no worry there.

I am curious only that we have a great @:setter and @:getter macro, but no corresponding @:super or some such thing.

Nicolas?

R

2011/10/19 Maximiliano Fernández <[hidden email]>
Only trough composition. For example:

var x (default,setX) : Float;
var compoSprite = new Sprite();

public function setX( inX : Float ) : Float {
    return x = compoSprite.x = inX;
}

That's the way I'm using it in translating a AS3 library to haXe. Also, I'm experimenting the way to make the composition trough Macro and it's kinda working, but I'm not sure if it's the best way of do it...

Max
0 1 0 | 0 0 1 | 1 1 1



--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Overriding setters

Franco Ponticelli
Have you tried super.setX(v) ?
I've never used Flash native modifiers so I am not sure if it works or not.

Franco

On Wed, Oct 19, 2011 at 7:58 AM, Russell Weir <[hidden email]> wrote:
Oh, I know all about composition, no worry there.

I am curious only that we have a great @:setter and @:getter macro, but no corresponding @:super or some such thing.

Nicolas?

R

2011/10/19 Maximiliano Fernández <[hidden email]>
Only trough composition. For example:

var x (default,setX) : Float;
var compoSprite = new Sprite();

public function setX( inX : Float ) : Float {
    return x = compoSprite.x = inX;
}

That's the way I'm using it in translating a AS3 library to haXe. Also, I'm experimenting the way to make the composition trough Macro and it's kinda working, but I'm not sure if it's the best way of do it...

Max
0 1 0 | 0 0 1 | 1 1 1



--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Overriding setters

Russell Weir
Yeah, I have tried every variation I can think of, with no luck.

Anything not in the extern http://haxe.org/api/flash9/display/displayobject is not going to compile anyway. x is a float, plain and simple... no set method etc.

It's just missing is all :(

On Wed, Oct 19, 2011 at 1:56 AM, Franco Ponticelli <[hidden email]> wrote:
Have you tried super.setX(v) ?
I've never used Flash native modifiers so I am not sure if it works or not.

Franco


On Wed, Oct 19, 2011 at 7:58 AM, Russell Weir <[hidden email]> wrote:
Oh, I know all about composition, no worry there.

I am curious only that we have a great @:setter and @:getter macro, but no corresponding @:super or some such thing.

Nicolas?

R

2011/10/19 Maximiliano Fernández <[hidden email]>
Only trough composition. For example:

var x (default,setX) : Float;
var compoSprite = new Sprite();

public function setX( inX : Float ) : Float {
    return x = compoSprite.x = inX;
}

That's the way I'm using it in translating a AS3 library to haXe. Also, I'm experimenting the way to make the composition trough Macro and it's kinda working, but I'm not sure if it's the best way of do it...

Max
0 1 0 | 0 0 1 | 1 1 1



--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Overriding setters

Nicolas Cannasse
In reply to this post by Russell Weir
Le 19/10/2011 08:58, Russell Weir a écrit :
> Oh, I know all about composition, no worry there.
>
> I am curious only that we have a great @:setter and @:getter macro, but
> no corresponding @:super or some such thing.
>
> Nicolas?

Yes, @:getter and @:setter are kind of hacks, they are not intended to
be used to override an existing setter. You would get an error anyway I
think because you need "override" flag.

Not sure if/how/when I can support it.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Overriding setters

Elsass Philippe
In reply to this post by Russell Weir
I'd like to +1 the need to make it possible to call the super class' setter.

Overriding setters is a very common use case in AS3:
- overriding width/height to bind component layout to size changes,
- always rounding the coordinates of a clip,
- etc.


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Overriding setters

Russell Weir
Oh, I've been overriding existing setters and getters with much success.

Tip: If you override a getter and flash throws a cast error, you are likely returning Dynamic when you should return {}. Franco figured that one out, after I banged my head on it a while.

Anyway, what I am saying is exactly what Elsass is. It's a "half way" perfect too, cause it does work... just needs a couple different byte codes now ;)

R

On Wed, Oct 19, 2011 at 6:51 AM, Elsass Philippe <[hidden email]> wrote:
I'd like to +1 the need to make it possible to call the super class' setter.

Overriding setters is a very common use case in AS3:
- overriding width/height to bind component layout to size changes,
- always rounding the coordinates of a clip,
- etc.


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org