|
| 1 | +--- |
| 2 | +author: hongbo |
| 3 | +date: "2020-09-23" |
| 4 | +previewImg: |
| 5 | +category: compiler |
| 6 | +title: What's new in ReScript 8.3 (Part 1) |
| 7 | +description: | |
| 8 | +--- |
| 9 | + |
| 10 | + |
| 11 | +# What's new in ReScript 8.3 (Part 1) |
| 12 | + |
| 13 | +ReScript is a soundly typed language with an optimizing compiler focused on JS platform. |
| 14 | +It's focused on type safety, performance and JS interop. It used to be called BuckleScript. |
| 15 | + |
| 16 | +[email protected] is available, people can try it via |
| 17 | + |
| 18 | +``` |
| 19 | + |
| 20 | +``` |
| 21 | + |
| 22 | +The changes are listed [here](https://github.com/rescript-lang/rescript-compiler/blob/master/Changes.md#83), this is a large release, and we will go through some highlighted changes. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +## Lightweight FFI attributes without `bs.` prefix |
| 27 | + |
| 28 | +In this release, we make the `bs.` prefix optional, this will make the FFI less verbose. |
| 29 | + |
| 30 | +For example, the old externals for `readFileAsUtf8Sync` used to be written like this |
| 31 | + |
| 32 | + |
| 33 | +<CodeTab labels={["ReScript", "Reason", "ML"]}> |
| 34 | +```res |
| 35 | +@bs.val @bs.module("fs") |
| 36 | +external readFileAsUtf8Sync: (string, @bs.as("utf8") _) => string = "readFileSync" |
| 37 | +``` |
| 38 | +```reason |
| 39 | +[@bs.val] [@bs.module "fs"] |
| 40 | +external readFileAsUtf8Sync: (string, [@bs.as "utf8"] _) => string = |
| 41 | + "readFileSync"; |
| 42 | +``` |
| 43 | +```ocaml |
| 44 | +external readFileAsUtf8Sync : string -> (_[@bs.as "utf8"]) -> string = "readFileSync" [@@bs.val] [@@bs.module "fs"] |
| 45 | +``` |
| 46 | +</CodeTab> |
| 47 | + |
| 48 | +It can now be simplified as |
| 49 | +<CodeTab labels={["ReScript", "Reason", "ML"]}> |
| 50 | +```res |
| 51 | +@val @module("fs") external readFileAsUtf8Sync: (string, @as("utf8") _) => string = "readFileSync" |
| 52 | +``` |
| 53 | +```reason |
| 54 | +[@val] [@module "fs"] |
| 55 | +external readFileAsUtf8Sync: (string, [@as "utf8"] _) => string = |
| 56 | + "readFileSync"; |
| 57 | +``` |
| 58 | +```ocaml |
| 59 | +external readFileAsUtf8Sync : string -> (_[@as "utf8"]) -> string = "readFileSync" |
| 60 | +[@@val] [@@module "fs"] |
| 61 | +``` |
| 62 | +</CodeTab> |
| 63 | + |
| 64 | +Note almost all previous attributes with `bs.xx` can be simplified as `xx` |
| 65 | +with the exception of the following two that don't have abbreviations: |
| 66 | + |
| 67 | +- `bs.send.pipe` : this attribute was deprecated in favor of `bs.send`; you can still use the existing one for backward compatibility. |
| 68 | + |
| 69 | +- `bs.splice` : this attribute was deprecated in favor of `bs.variadic`; you can still use the existing one for |
| 70 | +backward compatibility. |
| 71 | + |
| 72 | + |
| 73 | +## default import in Es6 support |
| 74 | + |
| 75 | +If you use es6 module output, the default bindings will be compiled properly now: |
| 76 | + |
| 77 | +<CodeTab labels={["ReScript", "Reason", "ML"]}> |
| 78 | +```res |
| 79 | +@module("hello") external input: string => string = "default" |
| 80 | +
|
| 81 | +let a = input("hello") |
| 82 | +``` |
| 83 | +```reason |
| 84 | +[@module "hello"] external input: string => string = "default"; |
| 85 | +
|
| 86 | +let a = input("hello"); |
| 87 | +``` |
| 88 | +```ocaml |
| 89 | +external input : string -> string = "default" [@@module "hello"] |
| 90 | +
|
| 91 | +let a = input "hello" |
| 92 | +``` |
| 93 | +</CodeTab> |
| 94 | + |
| 95 | +Will now be compiled properly under es6 format as below: |
| 96 | + |
| 97 | +```js |
| 98 | +import Hello from "hello"; |
| 99 | +var a = Hello("hello"); |
| 100 | +``` |
| 101 | + |
| 102 | + |
| 103 | +## Customized js file extension support |
| 104 | + |
| 105 | +Now user can pick up their js file extension support per module format: |
| 106 | + |
| 107 | +```json |
| 108 | + "package-specs": [{ |
| 109 | + "module": "es6", |
| 110 | + "suffix": ".mjs" |
| 111 | + },{ |
| 112 | + "module": "commonjs", |
| 113 | + "suffix": ".cjs" |
| 114 | + }], |
| 115 | + |
| 116 | +``` |
| 117 | + |
| 118 | +## More flexible filename support |
| 119 | + |
| 120 | +To have better integration with other [JS infrastructures](https://github.com/rescript-lang/rescript-compiler/issues/4624), |
| 121 | +for example, Next.js/React Native, we allow file names like `404.res`, |
| 122 | +`Button.Android.res` so that it can just be picked up by those tools |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +## Better type based inference for pattern `let {a,b,c} = value` |
| 127 | + |
| 128 | +Previously, for code like this: |
| 129 | + |
| 130 | +<CodeTab labels={["ReScript", "Reason", "ML"]}> |
| 131 | +```res |
| 132 | +module N = { |
| 133 | + type t = {x: int} |
| 134 | +} |
| 135 | +
|
| 136 | +let f = (u: N.t) => { |
| 137 | + let {x} = u |
| 138 | + x + 1 |
| 139 | +} /* type error */ |
| 140 | +``` |
| 141 | +```reason |
| 142 | +module N = { |
| 143 | + type t = {x: int}; |
| 144 | +}; |
| 145 | +
|
| 146 | +let f = (u: N.t) => { |
| 147 | + let {x} = u; |
| 148 | + x + 1; |
| 149 | +}; /* type error */ |
| 150 | +``` |
| 151 | +```ocaml |
| 152 | +module N = struct |
| 153 | + type t = { |
| 154 | + x : int |
| 155 | + } |
| 156 | +end |
| 157 | +
|
| 158 | +let f (u : N.t) = |
| 159 | + let {x } = u in x + 1 (* type error *) |
| 160 | +``` |
| 161 | +</CodeTab> |
| 162 | + |
| 163 | +You will get a type error |
| 164 | + |
| 165 | +``` |
| 166 | +Error: Unbound record field x |
| 167 | +``` |
| 168 | + |
| 169 | +However, since the compiler already knows the type of `u`, it is capable of looking up the label `x` properly. |
| 170 | +In this release, we make the original code style work out of the box without a work-around such as adding a module prefix |
| 171 | +like `let {N.x} = ..` |
| 172 | + |
| 173 | +## Build system enhancement |
| 174 | + |
| 175 | +A lot of work is put in improving the build system, we will expand on this topic in the next post! |
| 176 | + |
| 177 | +Happy Hacking! |
0 commit comments