COMMUNITY

[Solved] Interface merge type in haxe


(Uldis Baurovskis) #1

Hi,
have following code:

interface A {}
interface B {}
interface C {}

interface All extends A extends B extends C {}

var allObject: All;


function foo(source: A)...
function foo2(source: A + B)...
function foo3(source: B + C)...

foo(allObject);
foo2(allObject)
foo3(allObject)

Is it possible in haxe to make such types that object that implements interface All can be passed in all 3 functions?

In TypeScript it would be done like this

function foo2(source: A & B)...

This function would be satisfied by any object that implements both interfaces.

Thank you.


(Jens Fischer) #2

The Haxe equivalent for a TypeScript interface would actually be a typedef / structure. For those, the & syntax is indeed allowed as of Haxe 4 preview 4:


(Aleksandr Kuzmenko) #3
class Test {
    static function main() {
		var all:All = null;
        foo(all);
        foo2(all);
        foo3(all);
    }
            
    static function foo(source: A) {}
	static function foo2<T:(A,B)>(source:T) {}
	static function foo3<T:(B,C)>(source:T) {}
}

interface A {}
interface B {}
interface C {}

interface All extends A extends B extends C {}

https://try.haxe.org/#81cb9
That is for Haxe 3.4
For the latest Haxe 4.0-preview.4 replace (A,B) with A&B


(Uldis Baurovskis) #4

@Gama11 @RealyUniqueName Thank you for answers