haxe
haxe copied to clipboard
function binding and native optional arguments
class Main {
public static function main() {
var foo: (?a: Int, ?b: Int) -> Void = null;
foo(null, null); // throws on static targets
var bar = foo.bind(_, 0); // (?a: Null<Int>) -> Void
bar(null); // passes
}
}
I think in this case bar type should become (a: Int) -> Void instead as a default value for the native optional argument is lost.
To compensate stricter rules, maybe some construction can be used to specify default values in a binding:
var bar = foo.bind(_ = 0, 0); // (?a: Null<Int>) -> Void
This might be handy not only to this specific issue, but in general too.
Ok, i see the problem's origin - there are 3 types of optional arguments:
- haxe nullable arguments
(?a: Int),?Null<int>in old function notation - haxe non-nullable (but actually nullable) arguments
(a: Int = 0),?Int - external/native non-nullable arguments - no syntax/notation for those.
Seems like initially syntax for second one was supposed to be for third native type, but was later changed (#3826) in concept to be just syntax sugar for this:
function fun(a: Int = 1) {
// body with a as Int
}
// produces
function fun(a: Null<Int>) {
var a = a != null ? a : 1;
// body with a as Int
}
Documentation confuses things by saying that second notation is for third/native type, maybe it was not updated after the change was made. Also the fact that you can't pass a null to second one is strange, since there is no point in prohibiting it (again must be because of initial purpose).
The example in the opener of this issue is result of this, native optional argument should lose optionality, but currently (a: Int = 1) does not represent such. Proposal for some representation for those is here.
This is still a rather confusing situation, and reproduces like described. I don't know why "native" is being mentioned here at all, but I'd like to clean this up for Haxe 5 either way.