Closed
Description
There seems to be a few opportunities for semantic-based optimization for the compiler.
Here's an example of code generated via the new untagged variants functionality:
@unboxed
type rec t =
| @as(undefined) Missing
| @as(false) False
| @as(true) True
| @as(null) Null
| String(string)
| Number(float)
| Object(Js.Dict.t<t>)
| Array(array<t>)
type tagged_t =
| JSONFalse
| JSONTrue
| JSONNull
| JSONString(string)
| JSONNumber(float)
| JSONObject(Js.Dict.t<t>)
| JSONArray(array<t>)
let someJson: t = %raw(`'[{"name": "Haan"}, {"name": "Mr"}, false]'`)->JSON.parseExn->Obj.magic
let check = s =>
switch s {
| Array([True, False, Array([String("My name is"), Number(10.)])]) => Console.log("yup")
| _ => Console.log("Nope...")
}
function check(s) {
if (!(s instanceof Array) && typeof s !== "object" && typeof s !== "number" && typeof s !== "string") {
console.log("Nope...");
return ;
}
if (s instanceof Array) {
if (s.length !== 3) {
console.log("Nope...");
return ;
}
var match = s[0];
if (!(match instanceof Array) && typeof match !== "object" && typeof match !== "number" && typeof match !== "string" && match === true) {
var match$1 = s[1];
if (!(match$1 instanceof Array) && typeof match$1 !== "object" && typeof match$1 !== "number" && typeof match$1 !== "string" && match$1 === false) {
var match$2 = s[2];
if (!(match$2 instanceof Array) && typeof match$2 !== "object" && typeof match$2 !== "number" && typeof match$2 !== "string") {
console.log("Nope...");
return ;
}
if (match$2 instanceof Array) {
if (match$2.length !== 2) {
console.log("Nope...");
return ;
}
var match$3 = match$2[0];
if (!(match$3 instanceof Array) && typeof match$3 !== "object" && typeof match$3 !== "number" && typeof match$3 !== "string") {
console.log("Nope...");
return ;
}
if (typeof match$3 === "string" && match$3 === "My name is") {
var match$4 = match$2[1];
if (!(match$4 instanceof Array) && typeof match$4 !== "object" && typeof match$4 !== "number" && typeof match$4 !== "string") {
console.log("Nope...");
return ;
}
if (typeof match$4 === "number") {
if (match$4 !== 10) {
console.log("Nope...");
} else {
console.log("yup");
}
return ;
}
console.log("Nope...");
return ;
} else {
console.log("Nope...");
return ;
}
} else {
console.log("Nope...");
return ;
}
} else {
console.log("Nope...");
return ;
}
} else {
console.log("Nope...");
return ;
}
} else {
console.log("Nope...");
return ;
}
}
Notice for example if (!(match instanceof Array) && typeof match !== "object" && typeof match !== "number" && typeof match !== "string" && match === true) {
that could be just if (match === true)
.
cc @cristianoc