Is there a way to declare classes and methods abstract in Haxe?

Is there a way in Haxe to get the equivalent of abstract Java methods and abstract classes?

I want to

// An abstract class. (Written in a Java/Haxe hybrid.) abstract class Process<A> { public function then<B>( f : A -> Process<B> ) : Process<B> { var a : A = go() ; return f(a) ; } abstract public function go( ) : A ; } // A concrete class. class UnitP<A> extends Process<A> { var _a : A ; public function new( a : A ) { _a = a ; } public override function go() : A { return _a ; } } 

The closest I could get the definition of Process as an interface and implement it with the conceptually abstract class ProcessA , which defines both methods; the go implementation in ProcessA just crashes. Then I can extend my conceptually specific classes from ProcessA.

+6
source share
2 answers

As mentioned in MSGhero, Java style abstracts are not natively supported by Haxe. This was requested by several people, so Andy Lee wrote a macro to provide Haxe users with comparable functionality:

https://gist.github.com/andyli/5011520

+7
source

How would I do something equivalent in Haxe

  • Define a private constructor to ensure that an instance of the Abstract class has not been created.
  • Create an interface with all the methods that need to be implemented.
  • Create a class that inherits from Abstract and implements the interface.

     // An interface interface IProcess<A, B> { public function then( f : A -> AProcess<B> ) : AProcess<B>; public function go() : A; } // An abstract class. class AProcess<A, B> { private function new() {} public function then<B>( f : A -> AProcess<B> ) : AProcess<B> { var a : A = go() ; return f(a) ; } private function go() : A {}; } // A concrete class. class UnitP extends AProcess<A, B> implements IProcess { var _a : A ; public function new( a : A ) { super(); _a = a ; } public function go() : A { return _a ; } } 
0
source

Source: https://habr.com/ru/post/987679/


All Articles