Skip to content

Instantly share code, notes, and snippets.

@dlants
Last active April 19, 2021 15:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dlants/1247c5a6ab89342560fe40075646320a to your computer and use it in GitHub Desktop.
Save dlants/1247c5a6ab89342560fe40075646320a to your computer and use it in GitHub Desktop.
Generic deriving with purescript
-- an example of how to derive a show instance for a Maybe type
-- not totally sure why `derive instance showMyMaybe :: (Show a) => Show (MyMaybe a)` errors with...
-- error: CannotDerive :
-- Cannot derive a type class instance for Data.Show.Show (MyMaybe a) since instances of this type class are not derivable.
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
data MyMaybe a
= Just a
| Nothing
derive instance genericMyMaybe :: Generic (MyMaybe a) _
instance showMyMaybe :: (Show a) => Show (MyMaybe a) where show = genericShow
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
log $ show $ Just "hello, deriving!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment