Created
November 17, 2024 20:29
-
-
Save chrisdone/764d69639e36b9992a588abd488a82a2 to your computer and use it in GitHub Desktop.
variants fragment
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
-------------------------------------------------------------------------------- | |
-- Variants | |
-- NB: By the types here, you can't construct a @Variant NilL@. | |
data Variant xs where | |
LeftV :: forall k a xs. a -> Variant (ConsL k a xs) | |
RightV :: Variant xs -> Variant (ConsL k a xs) | |
-- Construction: | |
bar :: Variant (ConsL "String" Text (ConsL "Number" Double NilL)) | |
bar = RightV (LeftV 123) | |
-- Case analysis: | |
foo :: Variant (ConsL "String" Text (ConsL "Number" Double NilL)) -> Text | |
foo = \v -> | |
case v of | |
LeftV text -> text | |
RightV (LeftV double) -> Text.pack (show @Double double) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment