Is it possible to support embedded structures?
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]} |]
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
PyFClassifysuch 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.
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.
Hi @guibou , thank you for the detailed answer, it clarifies a few other questions I had (the one above was my work account)!