Skip to content

Commit

Permalink
Merge pull request #204 from roc-lang/custom-inspect
Browse files Browse the repository at this point in the history
Add Custom `Inspect` example for Opaque types
  • Loading branch information
Anton-4 authored Sep 4, 2024
2 parents b1771f2 + 9ac0b19 commit 5d4898e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ci_scripts/all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ go build -C examples/GoPlatform/platform -buildmode=pie -o dynhost
$ROC preprocess-host ./examples/GoPlatform/platform/dynhost ./examples/GoPlatform/platform/main.roc ./examples/GoPlatform/platform/libapp.so
$ROC build ./examples/GoPlatform/main.roc

$ROC test ./examples/CustomInspect/OpaqueTypes.roc

# temporarily allow failure of lsb_release in case it is not installed
set +e
os_info=$(lsb_release -a 2>/dev/null)
Expand Down
34 changes: 34 additions & 0 deletions examples/CustomInspect/OpaqueTypes.roc
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
15 changes: 15 additions & 0 deletions examples/CustomInspect/README.md
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
```
1 change: 1 addition & 0 deletions examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ You can find the source code for all of these at [github.com/roc-lang/examples](
- [Looping Tasks](/TaskLoop/README.html)
- [Record Builder](/RecordBuilder/README.html)
- [Encoding & Decoding Abilities](/EncodeDecode/README.html)
- [Custom Inspect](/CustomInspect/README.html)
- [Least Squares](/LeastSquares/README.html)
- [Towers of Hanoi](/TowersOfHanoi/README.html)
- [Multi-line Comments](/MultiLineComments/README.html)
Expand Down

0 comments on commit 5d4898e

Please sign in to comment.