How can I reflect the name of a type as a run time string from a proxy value?

I have a function f ∷ … ⇒ f α → Either String … — that is to say, the function may return an error description. It would be convenient if the error description included the name of the type α. So therefore I need a function like reflectTypeName ∷ Proxy α → String.

  • I found reflectSymbol that converts a proxy to a string — but it only works on a special kind Symbol.
  • I found reflect that converts a proxy to something — but I am not sure where to get the suitable instances for it.

So, there is a conspicuous omission of the function that I want. Is it found somewhere else? Or is it impossible to achieve with the current PureScript compiler?

Maybe something along the lines of typeclass on α would do what you want.

class Named α where
  name :: α -> String
  
instance nameString :: Named String where
  name _ = "String"

instance nameInt :: Named Int where
  name _ = "Int"

You can acquire the name of a constructor for types that are Generic, but I don’t think the type name is available that way.

1 Like