Here’s an example:
// rescript
type cat = {name: string}
let my_cat = {
name: "fluffy",
}
let cat_name = my_cat.name
Outputs:
// Generated by ReScript, PLEASE EDIT WITH CARE
var my_cat = {
name: "fluffy"
};
var cat_name = "fluffy";
export {
my_cat ,
cat_name ,
}
/* Not a pure module */
Instead of just transpiling from ReScript to JS, it’s improving the output since the compiler knows what the value of my_cat.name
is.
It can also do this with function calls:
// rescript
type cat = {name: string}
let my_cat = {
name: "fluffy",
}
let getName = cat => cat.name
let name = getName(my_cat)
Outputs:
// Generated by ReScript, PLEASE EDIT WITH CARE
function getName(cat) {
return cat.name;
}
var name = "fluffy";
var my_cat = {
name: "fluffy"
};
export {
my_cat ,
getName ,
name ,
}
/* No side effect */
If I use an object instead of a record, it does not do the same to the output:
// rescript
let my_cat = {
"name": "fluffy",
}
let getName = cat => cat["name"]
let name = getName(my_cat)
Outputs:
// Generated by ReScript, PLEASE EDIT WITH CARE
var my_cat = {
name: "fluffy"
};
function getName(cat) {
return cat.name;
}
var name = my_cat.name;
export {
my_cat ,
getName ,
name ,
}
/* name Not a pure module */
Some other quick example:
// rescript
let n = 1 + 1
// outputs
let n = 2
// rescript
let n = 1.0 +. 1.0
// outputs
let n = 1.0 + 1.0
// rescript
let t1 = [1, 2, 3][0]
// outputs
let t1 = 1
// rescript
let t1 = [1, 2, 3]
let t2 = t1[0]
// outputs
var t1 = [
1,
2,
3
];
var t2 = t1[0];
I’m sure there are other example.
Do we have this behavior documented anywhere?