Class-Interface problems

Hello there. I’m trying to harness classes and interfaces. I need something like class implementation with corresponding interface, where function uses either class (in normal implementation) or the interface (in the interface).
I felt that something like that should work:

class Test {
    static function main() {
        var tmp = new Foo();
        tmp.doSomething(tmp);
    }
}

class Foo implements IFoo {
	public function new() {}
    
    public function doSomething(tmp:Foo) {
        trace('FooBar');
    }
}

interface IFoo {
    public function doSomething(tmp:IFoo);
}

Class uses class, interface uses interface and class implements the interface, so should work, no ?

However compiler is giving me:

Build failure
Test.hx:17: characters 11-42 : Type required for extern classes and interfaces
Test.hx:11: lines 11-13 : Field doSomething has different type than in IFoo
Test.hx:17: characters 11-42 : Interface field is defined here
Test.hx:11: lines 11-13 : tmp : Foo -> Void should be tmp : IFoo -> Dynamic
Test.hx:11: lines 11-13 : Foo should be IFoo

Can anyone tell me why I can’t do it like that? Or how can I achieve simillar effect?

Thanks in advance :slight_smile:

First, functions in interface should declare return type like:

class Test {
interface IFoo {
public function doSomething(tmp:IFoo):Void;
}

Second, tmp is IFoo in IFoo.doSomething. so Foo should implements like:

class Foo implements IFoo {
public function new() {}
public function doSomething(tmp:IFoo) {
trace(‘FooBar’);
}
}

1 Like

Thanks @fnaith (also silly of me forgetting about Void).

1 Like