HaxeFoundation / haxe-evolution

Repository for maintaining proposal for changes to the Haxe programming language
111 stars 58 forks source link

implements for abstracts? Why not? #33

Closed malublu closed 6 years ago

malublu commented 6 years ago

Hi i think about a feature that haxe not support yet.

abstract NewClass (OldClass) implements AnInterface {
    public function test() {
        this.test();
    }
} 

Currently i do it like this:

class NewClass implements AnInterface {
     private var clazz:OldClass;

     public function test() {
         clazz.test();
     }
}

What you think about it? From: (https://github.com/HaxeFoundation/haxe/issues/6678)

Example:

class A {
    public function new() {}
    public function test() {}
}

interface ITestDebug {
    public function testDebug():Void;
}

abstract B (A) implements ITestDebug {
    public function new() {
        this = new A();
    }
    public function testDebug() {
        this.test();
    }
}

class Main {
    public static function main() {
        var b:B = new B();

        // b.test(); <- should not work because is it not visible anymore

        testDebugCall(b);
    }

    public static function testDebugCall(testDebug:ITestDebug) {
        testDebug.testDebug();
    }
}

And it can be compiled like an extends (but only for compile time):

class B extends A implements ITestDebug {
nadako commented 6 years ago

Abstracts are not run-time types and will only be represented at run-time as their underlying type (so always A in this case), thus they can't implement interfaces, which is a runtime dispatching mechanism.

nadako commented 6 years ago

Oh, and also this is not a proper evolution proposal, see README

piotrpawelczyk commented 6 years ago

Btw. what you probably want to do is @:forward(...) abstract NewClass(AnInterface) from AnInterface to AnInterface. Read about it in Haxe's manual - Forwarding abstract fields.