You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: crates/typing/README.md
+19-18Lines changed: 19 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,16 +34,16 @@ x: numeric <- 4
34
34
names:character[] <- c("Alice", "Bob")
35
35
```
36
36
37
-
However, this syntax would require major changes to R's parser and is not practical for early experimentation.
37
+
However, this syntax would require major changes to R's parser which is not practical for early experimentation.
38
38
39
39
### Option C: JSDoc-style comments
40
40
41
-
JSDoc is an alternative syntax for TypeScript that uses the same underlying type system and type checker. In this approach, types are provided in comments, making it practical for prototyping and not requiring changes to R itself.
41
+
[JSDoc](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html) is an alternative syntax for TypeScript that uses the same underlying type system and type checker. In this approach, types are provided in comments, so no changes to R itself are required.
42
42
43
43
```r
44
44
#: @param items character[] # character vector input
A key benefit of union types is that the type checker can enforce exhaustiveness in pattern matching or switch statements. This means that all possible variants of a union type must be handled explicitly, preventing bugs from unhandled cases.
265
266
266
-
For example, consider a `Maybe` type with two variants: `Just`and`Nothing`:
267
+
For example, suppose you have a function parameter that can be either `Just`or`Nothing`, representing a tagged union with two possible variants:
267
268
268
269
```r
269
-
#: @param result [Just numeric, Nothing]
270
+
#: @param maybe [Just numeric, Nothing]
270
271
#: @return numeric
271
-
process_result<-function(result) {
272
+
process_maybe<-function(maybe) {
272
273
switch(
273
-
tag(result),
274
-
Just=get_value(result),
274
+
tag(maybe),
275
+
Just=process(maybe),
275
276
Nothing=0
276
-
# If a new variant is added to the Maybe type, the type checker will report an error
277
-
# if it is not handled here.
277
+
# If a new variant is added to the `maybe` parameter,
278
+
#the type checker will report an error if it is not handled here.
278
279
)
279
280
}
280
281
```
281
282
282
-
If you later extend `Maybe`with a new variant (e.g., `Unknown`), the type checker will require you to update all switch statements that handle `Maybe` to cover the new case, ensuring your code remains correct and robust.
283
+
If you later extend the union with a new variant (e.g., `Unknown`), the type checker will require you to update all switch statements that handle `maybe` to cover the new case, ensuring your code remains correct and robust.
0 commit comments