Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 979
Theodore Norvell Avatar asked May 21 '15 14:05

Theodore Norvell


People also ask

Can we declare method as abstract?

A method declared using the abstract keyword within an abstract class and does not have a definition (implementation) is called an abstract method. When we need just the method declaration in a super class, it can be achieved by declaring the methods as abstracts.

Can a class be declared as abstract with all concrete methods?

No. Abstract class can have both an abstract as well as concrete methods. A concrete class can only have concrete methods. Even a single abstract method makes the class abstract.

How we can declare a class as abstract and why we need abstract classes?

A class containing abstract methods should also be abstract. We cannot create objects of an abstract class. To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstract class.

How can we declare a method in abstract class?

To declare an abstract method, use this general form: abstract type method-name(parameter-list); As you can see, no method body is present. Any concrete class(i.e. class without abstract keyword) that extends an abstract class must override all the abstract methods of the class.


2 Answers

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

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

like image 166
Malte Köhrer Avatar answered Nov 10 '22 04:11

Malte Köhrer


How I'd do something equivalent in Haxe

  • Set the constructor private to ensure no instance of the Abstract class is created.
  • Create an Interface with all methods that must be implemented
  • Create a class that inherits from the 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 ; }
    }
    
like image 29
bubblebenj Avatar answered Nov 10 '22 04:11

bubblebenj