Created
May 30, 2023 09:06
-
-
Save i-am-the-slime/990b6dbc14cd55a2fc4b3c0c14a9cea1 to your computer and use it in GitHub Desktop.
Courtesy of @purefunctor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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