Custom Inspect

An Opaque type can provide a custom implementation for the Inspect ability.

This can be useful for more complex types, or to hide internal implementation details.

Simple Tag Union


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_\""

Redacting a 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 *******\""