Skip to content

Instantly share code, notes, and snippets.

@i-am-the-slime
Created May 30, 2023 09:06
Show Gist options
  • Save i-am-the-slime/990b6dbc14cd55a2fc4b3c0c14a9cea1 to your computer and use it in GitHub Desktop.
Save i-am-the-slime/990b6dbc14cd55a2fc4b3c0c14a9cea1 to your computer and use it in GitHub Desktop.
Courtesy of @purefunctor
module Main where
import Prelude
import Effect (Effect)
import TryPureScript (p, render, text)
import Data.Generic.Rep (class Generic, Constructor, Sum(..), from)
import Data.Reflectable (class Reflectable, reflectType)
import Type.Proxy (Proxy(..))
data T = A Int | B | C
derive instance Generic T _
class NameOf t where
nameOf :: t -> String
instance Reflectable n String => NameOf (Constructor n a) where
nameOf _ = reflectType (Proxy :: _ n)
instance (NameOf a, NameOf b) => NameOf (Sum a b) where
nameOf = case _ of
Inl a -> nameOf a
Inr b -> nameOf b
main :: Effect Unit
main = render $ p $ text $ nameOf $ from B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment