Courtesy of @purefunctor

Mark Eibes
2023-05-30T09:06:06Z

NameOf.purs

| 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