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

Provide my own formatting of types via custom typeclass #91

Open
Profpatsch opened this issue Feb 12, 2022 · 1 comment
Open

Provide my own formatting of types via custom typeclass #91

Profpatsch opened this issue Feb 12, 2022 · 1 comment

Comments

@Profpatsch
Copy link
Contributor

Profpatsch commented Feb 12, 2022

I want to make it possible to have a simple derive clause for a newtype like

newtype Username = Username Text
  deriving newtype FormatQuote

and then automatically have that type implement PyFToString and all relevant mechanisms, so that when I write

let u = Username "myuser"
in [fmt|There was a problem with user {u}!|]

it will use a magical formatting that we define somehow, and the result will be:

There was a problem with user "myuser"

where the quotes get automatically added by the way FormatQuote is implemented.

I don’t know if this is possible at all, but it would be cool to have somehow.

I guess the closest is the following snippet:

class FormatQuote a where
  q :: a -> FormatQuoteT

instance FormatQuote Text where
  q t = FormatQuoteT $ textToString t

newtype FormatQuoteT = FormatQuoteT String

type instance PyF.PyFClassify FormatQuoteT = 'PyF.PyFString

instance PyF.PyFToString FormatQuoteT where
  pyfToString (FormatQuoteT s) =  "\"" <> s <> "\""

which lets me do

let u = Username "myuser" in [fmt|{q u}|]
"\"myuser\""

however, this means I always have to remember to use q and also the q symbol is not available in any of my code.

@guibou
Copy link
Owner

guibou commented Mar 17, 2022

Hi @Profpatsch, thank you for this issue (and sorry for my silence).

I will have a look. Right now you need to make instances for a few classes / type families, but maybe we can do better.

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

No branches or pull requests

2 participants