Skip to content

Commit a6b44f9

Browse files
authored
Fix for desugaring: keeping reference mutability intact (#60)
1 parent 218e962 commit a6b44f9

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,13 @@ const result = try expr1
153153
This is "equivalent" to:
154154

155155
```js
156-
let result
156+
let _result
157157
try {
158-
result = Result.ok(expr1)
158+
_result = Result.ok(expr1)
159159
} catch (error) {
160-
result = Result.error(error)
160+
_result = Result.error(error)
161161
}
162+
const result = _result
162163
```
163164

164165
### Any valid expression can be use
@@ -170,14 +171,15 @@ const result = try data?.someProperty.anotherFunction?.(await someData()).andAno
170171
This is "equivalent" to:
171172
172173
```js
173-
let result
174+
let _result
174175
try {
175-
result = Result.ok(
176+
_result = Result.ok(
176177
data?.someProperty.anotherFunction?.(await someData()).andAnotherOne()
177178
)
178179
} catch (error) {
179-
result = Result.error(error)
180+
_result = Result.error(error)
180181
}
182+
const result = _result
181183
```
182184
183185
`try` cannot nest since its a statement.
@@ -191,12 +193,13 @@ const result = try await fetch("https://api.example.com/data")
191193
This is "equivalent" to:
192194
193195
```js
194-
let result
196+
let _result
195197
try {
196-
result = Result.ok(await fetch("https://api.example.com/data"))
198+
_result = Result.ok(await fetch("https://api.example.com/data"))
197199
} catch (error) {
198-
result = Result.error(error)
200+
_result = Result.error(error)
199201
}
202+
const result = _result
200203
```
201204
202205
### Statements are not expressions
@@ -209,12 +212,13 @@ const result = try using resource = new Resource() // Syntax error!
209212
This is because their "equivalent" would also result in a syntax error:
210213
211214
```js
212-
let result
215+
let _result
213216
try {
214-
result = Result.ok(throw new Error("Something went wrong")) // Syntax error!
217+
_result = Result.ok(throw new Error("Something went wrong")) // Syntax error!
215218
} catch (error) {
216-
result = Result.error(error)
219+
_result = Result.error(error)
217220
}
221+
const result = _result
218222
```
219223
220224
A detailed discussion about this topic is available at [GitHub Issue #54](https://github.com/arthurfiorette/proposal-try-statements/issues/54) for those interested.

0 commit comments

Comments
 (0)