possible ? : function returning "this" in base class return the actuall this of the extending class

54 views
Skip to first unread message

Thomas John

unread,
Oct 18, 2016, 9:57:23 AM10/18/16
to Haxe
Hello

everything is in the title but let me explain this a little better with an example:
(You can run it in http://try.haxe.org/)

class Test {
    static function main() {
        trace("Haxe is great!");
       
        var extendedClass:ExtendedClass = new ExtendedClass();
        extendedClass.init().start(); // that obviously gives me an error as BaseClass has no field start
    }
}

class BaseClass {
    public function new()
    {
       
    }
   
    public function init():BaseClass
    {
        trace("BaseClass init !");
        return this;
    }
}

class ExtendedClass extends BaseClass {
   
    public function start():ExtendedClass
    {
        trace("ExtendedClass start !");
        return this;
    }
}

So the comment in red explains what the problem is. BaseClass does not have a start field to call. And it's perfectly normal.
I guess it would run perfectly if the compiler wasn't there to check that there might be an error.
What would be cool is, instead of specifying BaseClass as the returning object type of a function, specify some kind of a special type (like "This") so the compiler knows that it is the actual extended object (the final one) and not just the BaseClass...

Hope I made myself clear enough.
Maybe I could achieve that with macros ?
I would gladly accept any help for that :)

Thanks.

Thomas.

Dan Korostelev

unread,
Oct 18, 2016, 10:19:04 AM10/18/16
to Haxe
There's an issue about this: https://github.com/HaxeFoundation/haxe/issues/4474

вторник, 18 октября 2016 г., 16:57:23 UTC+3 пользователь Thomas John написал:

PSvils

unread,
Oct 18, 2016, 4:02:38 PM10/18/16
to Haxe
In this case though, what happens if you have:

var base:BaseClass = new ExtendedClass();
base.init();

?

Thomas John

unread,
Oct 18, 2016, 5:00:15 PM10/18/16
to haxe...@googlegroups.com
What I want is to have the type returned by BaseClass.init the same as the extending class -> ExtendedClass.

Dan Korostelev pointed to an "issue" on the haxe github that would solve the problem if it was implemented -> the Self type.

--
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 a topic in the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
Thomas John

JLM

unread,
Oct 19, 2016, 5:26:39 AM10/19/16
to Haxe
Thomas

Found this works....

class ExtendedClass extends BaseClass {
   
   
public function start():ExtendedClass
   
{
        trace
("ExtendedClass start !");
       
return this;
   
}

   
override public function init(): ExtendedClass
   
{
       
return cast super.init();
   
}
}

but I can see that's not ideal. A simple workround.

cast( extendedClass.init(), ExtendedClass ).start();


Best

Justin
Reply all
Reply to author
Forward
0 new messages