There’s a workaround for that:
public static function openWindow(?cls:Class<BasicWindow>, ?inst:BasicWindow):Void {
if(cls != null) {
inst = Type.createInstance(cls, []);
}
inst.init();
}
Because the parameters are optional, you can either call openWindow(FancyWindow)
or openWindow(new FancyWindow())
. In the first case, you’re passing a Class<FancyWindow>
object, so it’ll be assigned to cls
. In the second case, you’re passing a FancyWindow
instance, so it’ll be assigned to inst
.
The unused argument will be null. Check which one is null, and use the other. (In the code above, I’m assuming that at least one argument is defined. In fact, think of that as a precondition of the function.)
You can use generics (as described above) if you need this function’s return type to match what it’s given.