官术网_书友最值得收藏!

Understanding type erasure

To concretely grasp the effect of static/dynamic separation, let's look at type erasure, which is something that happens when we compile the preceding code to JavaScript. The following is the output with all redundant comments removed:

// src/Ch02/Ch02_Demo.bs.js
var bob = [1, "Bob"];
var acmeCo_002 = [bob, 0];
var acmeCo = [1, "Acme Co.", acmeCo_002];

As we mentioned earlier, BuckleScript compiles Reason record types into JavaScript arrays with the corresponding number of elements. BuckleScript, in fact, performs quite a number of optimizations for you. Some of these come from its underlying OCaml compiler technology, which has been developed since the 1990s, but other things are quite unique in the world of language-to-JavaScript compilers.

Notice that BuckleScript has wiped out both the type definitions and has output only the minimum number of values it actually needs for runtime. The important thing to understand here is that all the output values follow the laws introduced by their corresponding types; for example, the Bob value, of type person, can only be an array with two elements (a number and a string, corresponding to the two fields in the person record), and the acmeCo value can only be an array with three elements of the correct types. Anything else is impossible – with a mathematical degree of certainty – even in output JavaScript code, because code that doesn't pass the typing rules (that is, doesn't typecheck) would not even compile.

主站蜘蛛池模板: 溧水县| 揭阳市| 卢湾区| 鹿泉市| 永平县| 临桂县| 阿鲁科尔沁旗| 唐山市| 得荣县| 芷江| 开封县| 于田县| 高阳县| 金山区| 明光市| 通许县| 沙坪坝区| 屯门区| 邢台县| 盘山县| 长岭县| 兴化市| 皮山县| 威信县| 若羌县| 德格县| 河源市| 磐安县| 抚宁县| 白银市| 申扎县| 安岳县| 辰溪县| 唐山市| 仁布县| 新郑市| 大城县| 突泉县| 比如县| 象山县| 银川市|