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

Is it possible to support embedded structures? #72

Open
coderfromhere opened this issue Apr 9, 2021 · 3 comments
Open

Is it possible to support embedded structures? #72

coderfromhere opened this issue Apr 9, 2021 · 3 comments

Comments

@coderfromhere
Copy link

As Python interpolation supports embedded expressions, I wonder if it's possible for PyF to support something like:

[fmt| this is an embedded list: {[varA, varB]} |]
@guibou
Copy link
Owner

guibou commented Apr 19, 2021

Hello.

I do apologize for the time it takes to answer.

That's a good question. In python:

>>> f"hello {[1,2]}"
'hello [1, 2]'

In PyF, you can use the s modifier in order to force the value to a string:

Prelude PyF> :set -XTemplateHaskell -XQuasiQuotes
Prelude PyF> [fmt|hello {[1,2]:s}|]
"hello [1,2]"

However, making it to work without the s modifier is a bit more tricky. You can either:

  • write you own instance for PyFClassify such as:
Prelude PyF> :set -XTypeFamilies
Prelude PyF> type instance PyFClassify [t] = 'PyFString
Prelude PyF> [fmt|hello {[1,2]}|]
"hello [1,2]"

However, this has to be done for all instances.

There is no support in Haskell for "overlappable" type instances, so we cannot write something such as type instance PyFClassify t = 'PyFString and overlap the result for other types. We can however use a closed type familly, but it will forbid users to add their own PyFClassify instances for custom types.

@guibou
Copy link
Owner

guibou commented Apr 19, 2021

I tried to add an OVERLAPABLE instance:

instance {-# OVERLAPPABLE #-} (FormatAny2 (PyFClassify t) t k) => FormatAny t k where
  formatAny = formatAny2 (Proxy :: Proxy (PyFClassify t))

But it generates conflicts and now any Showable is displayed as string by default. This is not a wanted behavior.

I'll try to think about a solution, in the meantime, you can use the s modifier or create your own type instance for PyFClassify.

@avanov
Copy link

avanov commented Apr 20, 2021

Hi @guibou , thank you for the detailed answer, it clarifies a few other questions I had (the one above was my work account)!

@guibou guibou closed this as completed Dec 11, 2021
@guibou guibou reopened this Dec 11, 2021
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

3 participants