haxe

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


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

What I want is

// 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've been able to get is to define Process as an interface and to implement it with a conceptually abstract class ProcessA, which defines both methods; the implementation of go in ProcessA simply crashes. Then I can extend my conceptually concrete classes off ProcessA.


Solution

  • Haxe 4.2 (2021) introduced abstract classes, with syntax very much like proposed in this question:

    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 ; }
    
        // (note: no `override`)
        public function go() : A { return _a ; }
    }
    

    And if you were to omit declaration of go(), you would receive

    src/Test.hx:31: characters 6-11 : This class extends abstract class Process but doesn't > implement the following method
    src/Test.hx:31: characters 6-11 : Implement it or make UnitP abstract as well
    src/Test.hx:27: characters 29-31 : ... go()