fix whenLet (for all targets, I think). close #64

This commit is contained in:
2022-08-02 00:19:22 +00:00
parent 90f4368a59
commit 31afc7f912
3 changed files with 32 additions and 36 deletions

View File

@@ -689,22 +689,25 @@ class Macros {
b.symbol("null"); b.symbol("null");
}; };
return b.callSymbol("case", [ var gensym = b.symbol();
firstValue, return b.let(
b.call( [gensym, firstValue],
firstPattern, [ [b.callSymbol("case", [
if (bindingList.length == 0) { gensym,
thenExp; b.call(
} else { b.callSymbol("when", [gensym, firstPattern]), [
ifLet(assertLet, wholeExp, [ if (bindingList.length == 0) {
b.list(bindingList) thenExp;
].concat(exps.slice(1)), k); } else {
} ifLet(assertLet, wholeExp, [
]), b.list(bindingList)
b.callSymbol("otherwise", [ ].concat(exps.slice(1)), k);
}
]),
b.callSymbol("otherwise", [
elseExp elseExp
]) ])
]); ])]);
} }
macros["ifLet"] = ifLet.bind(false); macros["ifLet"] = ifLet.bind(false);
@@ -1177,10 +1180,8 @@ class Macros {
b.callField(funcName, exps.shift(), exps), b.callField(funcName, exps.shift(), exps),
b.callSymbol("-1", [b.symbol("haxe.ds.Option.None")]), b.callSymbol("-1", [b.symbol("haxe.ds.Option.None")]),
b.callSymbol("other", [b.callSymbol("haxe.ds.Option.Some", [b.symbol("other")])]), b.callSymbol("other", [b.callSymbol("haxe.ds.Option.Some", [b.symbol("other")])]),
b.callSymbol("null", [b.callSymbol("throw", [b.str("Haxe indexOf is broken")])])
]; ];
if (!(Context.defined('cs') || Context.defined('cpp'))) {
cases.push(b.callSymbol("null", [b.callSymbol("throw", [b.str("Haxe indexOf is broken")])]));
}
return b.callSymbol("case", cases); return b.callSymbol("case", cases);
} }
macros["indexOf"] = indexOfMacro.bind(false); macros["indexOf"] = indexOfMacro.bind(false);

View File

@@ -349,6 +349,13 @@ class Prelude {
public static var joinPath:Function = Reflect.makeVarArgs(_joinPath); public static var joinPath:Function = Reflect.makeVarArgs(_joinPath);
public static function isNull(v:Any) {
return switch (Type.typeof(v)) {
case TNull: true;
default: false;
}
}
public static dynamic function truthy<T>(v:T) { public static dynamic function truthy<T>(v:T) {
return switch (Type.typeof(v)) { return switch (Type.typeof(v)) {
case TNull: false; case TNull: false;

View File

@@ -338,22 +338,7 @@ class SpecialForms {
var exp = k.withoutListWrapping().convert(args[0]); var exp = k.withoutListWrapping().convert(args[0]);
// On C#, C++, and HashLink, value types (specifically Float and Int) cannot be null, so they cannot be compared with null. var canCompareNull = !isTupleCase;
// Therefore a null case doesn't need to be added--and will cause a compile failure if it is.
var canCompareNull = !isTupleCase &&
if (Context.defined('cs') || Context.defined('cpp') || Context.defined('hl')) {
var type = exp.typeof(k.typeHints);
switch (type) {
case null:
false;
case Success(TAbstract(ref, [])) if (["Int", "Float", "Bool"].indexOf(ref.get().name) != -1):
false;
case Failure(_):
KissError.warnFromExp(args[0], "Can't detect whether expression can be null-checked");
false;
default: true;
}
} else true;
var cases = args.slice(1); var cases = args.slice(1);
// case also override's haxe's switch() behavior by refusing to match null values against <var> patterns. // case also override's haxe's switch() behavior by refusing to match null values against <var> patterns.
@@ -375,8 +360,11 @@ class SpecialForms {
throw KissError.fromExp(wholeExp, "Unmatched pattern: null"); throw KissError.fromExp(wholeExp, "Unmatched pattern: null");
} }
var nullCase = b.callSymbol("null", [b.raw(nullExpr.toString())]); var nullCase = if (k.hscript) {
b.callSymbol("null", [b.raw(nullExpr.toString())]);
} else {
b.call(b.callSymbol("when", [b.callSymbol("Prelude.isNull", [b.symbol("v")]), b.symbol("v")]), [b.raw(nullExpr.toString())]);
};
cases.insert(0, nullCase); cases.insert(0, nullCase);
} }