-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from roc-lang/custom-inspect
Add Custom `Inspect` example for Opaque types
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module [] | ||
|
||
### start snippet color | ||
Color := [ | ||
Red, | ||
Green, | ||
Blue, | ||
] | ||
implements [ | ||
Inspect { toInspector: colorInspector }, | ||
] | ||
|
||
colorInspector : Color -> Inspector f where f implements InspectFormatter | ||
colorInspector = \@Color color -> | ||
when color is | ||
Red -> Inspect.str "_RED_" | ||
Green -> Inspect.str "_GREEN_" | ||
Blue -> Inspect.str "_BLUE_" | ||
|
||
expect Inspect.toStr (@Color Red) == "\"_RED_\"" | ||
expect Inspect.toStr (@Color Green) == "\"_GREEN_\"" | ||
expect Inspect.toStr (@Color Blue) == "\"_BLUE_\"" | ||
### end snippet color | ||
|
||
### start snippet secret | ||
MySecret := Str implements [ | ||
Inspect { toInspector: mySecretInspector }, | ||
] | ||
|
||
mySecretInspector : MySecret -> Inspector f where f implements InspectFormatter | ||
mySecretInspector = \@MySecret _ -> Inspect.str "******* REDACTED *******" | ||
|
||
expect Inspect.toStr (@MySecret "password1234") == "\"******* REDACTED *******\"" | ||
### end snippet secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Custom Inspect | ||
|
||
An [Opaque type](https://www.roc-lang.org/tutorial#opaque-types) can provide a custom implementation for the [Inspect](https://www.roc-lang.org/builtins/Inspect) ability. | ||
|
||
This can be useful for more complex types, or to hide internal implementation details. | ||
|
||
## Simple Tag Union | ||
```roc | ||
file:OpaqueTypes.roc:snippet:color | ||
``` | ||
|
||
## Redacting a Secret | ||
```roc | ||
file:OpaqueTypes.roc:snippet:secret | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters