Skip to content

Sum types with Generics-Rep instances to Variant for free!

License

Notifications You must be signed in to change notification settings

justinwoo/purescript-kishimen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PureScript-Kishimen

aka PureScript-Dan-Abramov

Sum types with Generics-Rep instances to Variant for free!

Usage

First, make a sum type of zero or one argument constructors, and derive generic:

data Fruit = Apple | Banana Int | Kiwi String
derive instance genericFruit :: Generic Fruit _

Then use genericSumToVariant:

main :: Effect Unit
main = do
  logShow $ genericSumToVariant Apple
  logShow $ genericSumToVariant (Banana 3)
  logShow $ genericSumToVariant (Kiwi "green")

  -- results:
  -- (inj @"Apple" {})
  -- (inj @"Banana" 3)
  -- (inj @"Kiwi" "green")

  log $ JSON.writeJSON $ genericSumToVariant Apple
  log $ JSON.writeJSON $ genericSumToVariant (Banana 3)
  log $ JSON.writeJSON $ genericSumToVariant (Kiwi "green")

  -- results:
  -- {"type":"Apple","value":{}}
  -- {"type":"Banana","value":3}
  -- {"type":"Kiwi","value":"green"}

Amazing!

How?

You can see the inferred type in the expectInferred test:

test1 :: Unit
test1 = expectInferred expectedP simpleValue
  where
    expectedP = Proxy :: _
      (Variant
        ( "Apple" :: {}
        , "Banana" :: Int
        , "Kiwi" :: String
        ))
    simpleValue = genericSumToVariant Apple

About

Sum types with Generics-Rep instances to Variant for free!

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published