Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#14] Add type family to display list of types separated by commas #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dalpd
Copy link

@dalpd dalpd commented Oct 24, 2021

Hi there!

I initially took a stab at this during summer break but I never got a chance to get back to it, that is until today.

I previously was thinking about asking what to do with Maybe a and [a] as they were represented as Maybe and [] when left to the Generic instance but I think that's no good so also added them in TypeName.

Copy link
Contributor

@chshersh chshersh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dalpd,

Thanks for your contribution! I described an alternative design for this problem, which I believe will result in a smaller and cleaner code. But I hope that you were able to practice type-level programming in Haskell and learned something new nevertheless from working on this issue 🤗

Comment on lines +121 to +131
-- | Internal type family to go from a `Type` to a `Symbol` representation.
-- The names for types without `Generic` instances have to be defined
-- manually, and we do so for a handful of common types.
type family TypeName (a :: Type) :: Symbol where
TypeName Char = "Char"
TypeName String = "String"
TypeName Int = "Int"
TypeName (Maybe a) = AppendSymbol "Maybe " (TypeName a)
TypeName [a] = AppendSymbol "[" (AppendSymbol (TypeName a) "]")
TypeName (D1 ('MetaData name _ _ _) _ _) = name
TypeName a = TypeName (Rep a ())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach is not extensible. Users of type-errors-pretty cannot create new instances of TypeName. And the library cannot provide instances for all types 😞

To solve this problem nicely, the CommaSeparated type family can return a type of kind ErrorMessage instead of Symbol. The type-level operator <> already converts types and type-level symbols to a nice stringified representation that can be later reused in error messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants