Skip to content

Instantly share code, notes, and snippets.

@chrisdone
Created November 17, 2024 20:29
Show Gist options
  • Save chrisdone/764d69639e36b9992a588abd488a82a2 to your computer and use it in GitHub Desktop.
Save chrisdone/764d69639e36b9992a588abd488a82a2 to your computer and use it in GitHub Desktop.
variants fragment
--------------------------------------------------------------------------------
-- 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