COMMUNITY

Why does the compiler keep interfaces in generated JS

haxe-js

(Ben Roberts) #1

If you look at https://try.haxe.org/#585c3 and open the JS source tab you’ll see that the interface defined in the following Haxe code…

interface IMyClass {
    var myVar:String;
}

class MyClass implements IMyClass {
    static function main(){}
	public var myVar:String;
}

… appears in the generated JS…

// Generated by Haxe 3.4.4
(function () { "use strict";
var IMyClass = function() { };
var MyClass = function() { };
MyClass.__interfaces__ = [IMyClass];
MyClass.main = function() {
};
MyClass.main();
})();

What is the purpose of generating interface related code in JS? I understand that interfaces are use for type checking at build time but I would expect that once the JS has been generated then any interface related code could be stripped in order to minimise the JS payload.


(Dan Korostelev) #2

It is used for run-time type checking such as Std.is, but I guess we could skip generating this in case Std.is itself is not present in the output…


(Rudy Ges) #3

What @nadako just said (damn, you’re quick :D)

You can add @:remove to your interface to avoid this.


(Dan Korostelev) #4

I have an advantage of having a slack notification bot :stuck_out_tongue:


(Ben Roberts) #5

Thanks for the quick responses. Makes sense. Although adding @:remove to an interface could apparently break any future run-time type checking in a hard to debug way. You’d have to remember to remove the @:remove once you use Std.is


(Dan Korostelev) #6

I created an issue on the Haxe repo so I don’t forget to look into it :slight_smile:


(Dan Korostelev) #7

Haxe 4 will be a bit smarter with this. Thanks for bringing this topic up :slight_smile: