Skip to content

Commit 69bd74c

Browse files
committed
add different syntaxes
1 parent e6b84d2 commit 69bd74c

File tree

2 files changed

+172
-2
lines changed

2 files changed

+172
-2
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 it optional to have `bs.` prefix it or not, 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+
```ocaml
34+
external readFileAsUtf8Sync : string -> (_[@bs.as "utf8"]) -> string = "readFileSync" [@@bs.val] [@@bs.module "fs"]
35+
```
36+
37+
Could be simplified as
38+
```ocaml
39+
external readFileAsUtf8Sync : string -> (_[@as "utf8"]) -> string = "readFileSync"
40+
[@@val] [@@module "fs"]
41+
```
42+
43+
Note almost all previous attributes with `bs.xx` can be simplified as `xx`, except such two that
44+
we don't provide abbreviations:
45+
46+
- `bs.send.pipe` : this attribute was deprecated in favor of `bs.send`, you can still use the existing one for backward compatibility.
47+
48+
- `bs.splice` : this attribute was deprecated in favor of `bs.variadic`, you can still use the existing one for
49+
backward compatibility.
50+
51+
52+
## default import in Es6 support
53+
54+
If you use es6 module output, the default bindings will be compiled properly now:
55+
56+
```ocaml
57+
external input : string -> string = "default" [@@module "hello"]
58+
59+
let a = input "hello"
60+
```
61+
62+
Will now be compiled properly under es6 format as below:
63+
64+
```js
65+
import Hello from "hello";
66+
var a = Hello("hello");
67+
```
68+
69+
70+
## Customized js file extension support
71+
72+
Now user can pick up their js file extension support per module format:
73+
74+
```json
75+
"package-specs": [{
76+
"module": "es6",
77+
"suffix": ".mjs"
78+
},{
79+
"module": "commonjs",
80+
"suffix": ".cjs"
81+
}],
82+
83+
```
84+
85+
## More flexible filename support
86+
87+
To have better integration with other [JS infrastructures](https://github.com/rescript-lang/rescript-compiler/issues/4624), for example, Next.js/React Native, we allow file names like `404.res`, `Button.Andriod.res` so that it can just be picked up by those tools
88+
89+
90+
91+
## Better type based inference for pattern `let {a,b,c} = value`
92+
93+
Previously for such piece of code:
94+
95+
```ocaml
96+
module N = struct
97+
type t = {
98+
x : int
99+
}
100+
end
101+
102+
let f (u : N.t) =
103+
let {x } = u in x + 1 (* type error *)
104+
```
105+
106+
You will get a type error
107+
108+
```
109+
Error: Unbound record field x
110+
```
111+
112+
However, since the compiler already knows the type of `u`, it is capable of looking up the label `x` properly,
113+
in this release, we make such style work out of the box without the work around like adding a module prefix like `let {N.x} = ..`
114+
115+
## Build system enhancement
116+
117+
A lot of work is put in improving the build system, we will expand on this topic in the next post!
118+
119+
Happy Hacking!

_blogposts/2020-09-23-release-8-3.mdx

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,36 @@ In this release, we make it optional to have `bs.` prefix it or not, this will m
3030
For example, the old externals for `readFileAsUtf8Sync` used to be written like this
3131

3232

33+
<CodeTab labels={["ReScript", "Reason", "ML"]}>
3334
```res
3435
@bs.val @bs.module("fs")
3536
external readFileAsUtf8Sync: (string, @bs.as("utf8") _) => string = "readFileSync"
36-
3737
```
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>
3847

3948
Could be simplified as
49+
<CodeTab labels={["ReScript", "Reason", "ML"]}>
4050
```res
4151
@val @module("fs") external readFileAsUtf8Sync: (string, @as("utf8") _) => string = "readFileSync"
42-
4352
```
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>
4463

4564
Note almost all previous attributes with `bs.xx` can be simplified as `xx`, except such two that
4665
we don't provide abbreviations:
@@ -55,12 +74,23 @@ backward compatibility.
5574

5675
If you use es6 module output, the default bindings will be compiled properly now:
5776

77+
<CodeTab labels={["ReScript", "Reason", "ML"]}>
5878
```res
5979
@module("hello") external input: string => string = "default"
6080
6181
let a = input("hello")
82+
```
83+
```reason
84+
[@module "hello"] external input: string => string = "default";
6285
86+
let a = input("hello");
6387
```
88+
```ocaml
89+
external input : string -> string = "default" [@@module "hello"]
90+
91+
let a = input "hello"
92+
```
93+
</CodeTab>
6494

6595
Will now be compiled properly under es6 format as below:
6696

@@ -95,6 +125,7 @@ To have better integration with other [JS infrastructures](https://github.com/re
95125

96126
Previously for such piece of code:
97127

128+
<CodeTab labels={["ReScript", "Reason", "ML"]}>
98129
```res
99130
module N = {
100131
type t = {x: int}
@@ -104,8 +135,28 @@ let f = (u: N.t) => {
104135
let {x} = u
105136
x + 1
106137
} /* type error */
138+
```
139+
```reason
140+
module N = {
141+
type t = {x: int};
142+
};
107143
144+
let f = (u: N.t) => {
145+
let {x} = u;
146+
x + 1;
147+
}; /* type error */
148+
```
149+
```ocaml
150+
module N = struct
151+
type t = {
152+
x : int
153+
}
154+
end
155+
156+
let f (u : N.t) =
157+
let {x } = u in x + 1 (* type error *)
108158
```
159+
</CodeTab>
109160

110161
You will get a type error
111162

0 commit comments

Comments
 (0)