PyF icon indicating copy to clipboard operation
PyF copied to clipboard

Hooks to customize interpolation

Open TikhonJelvis opened this issue 2 years ago • 0 comments

It would be great to have a postProcess-style hook that applied to each interpolated expression. This would make it much easier to customize the exact behavior of the quasiquoter while reusing the interpolation and Haskell-expression-parsing machinery from this package.

I haven't really thought the design through yet, but it would be neat if the Python formatting DSL could be factored out as a function—that would leave people with a lot of flexibility around how to handle interpolation.

I've run into two situations where this level of flexibility would have been useful:

  1. Integrating with some class to display user-facing values (ie Display or something internal)

  2. Interpolating for code generation where it would be nice to restrict expressions to a domain-specific newtype or even automatically manage language-specific things like quoting and name resolution

Unfortunately, none of the Haskell interpolation libraries I've looked at are flexible enough for this kind of use.

Is this kind of usecase something you'd be interested in supporting in PyF? If it is, I'm happy to try my hand at writing a PR, although I'd appreciate suggestions on how to fit it into the existing design of the library. Alternatively, I could see this fitting more into a library for writing quasiquoters rather than PyF, but I'm not sure if any library like that currently exists.

TikhonJelvis avatar Jun 11 '22 20:06 TikhonJelvis