-
Notifications
You must be signed in to change notification settings - Fork 30
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
Add basic string interpolation / string templates #743
base: master
Are you sure you want to change the base?
Conversation
This is a great first implementation, but I'm not sure that desugaring in parser is the best choice here :/ Personally, I'd much rather see templates up until the transformation to core so that we can:
|
You raise some very good concerns and I completely agree. I will try to go the 'non-lazy route' and I actually completely thread through templates to core. |
Don't get me wrong, having a version is still super helpful; I truly have no idea how difficult it really is to do the thing I outlined above. But I hope it's worth it, especially wrt technical debt. :) |
I am not completely against desugaring in parser, but maybe we could actually desugar
to something like:
this way, as a user I can still overload |
I just pushed a prototype that threads through string templates all the way until code generation. This has the added benefit that we remain somewhat flexible for later changes, as Jiří mentioned. Furthermore, we can re-use the string interpolation capabilities of each respective backend, e.g. currently the JS string templates are generated as template literals. As a trade-off, we need to add a Note: I currently only fully implemented it for JS. If we come to an agreement that this is the right way to proceed, I will implement for the remaining backends. (I already started working on this before Jonathan's remark and I'd rather discuss this implementation briefly before scrapping it right away :) ) |
This relates to some strategy we'll need to come up with in order to represent custom string interpolators (which is very outside of the scope of this PR, I believe...) Then I'd expect some desugaring like, for example: val name = "Joe"
val age = 42
////
val s: SQL::Stmt =
sql"SELECT * FROM Person p WHERE p.last_name = ${name} AND p.age = ${age}"
// ~>
val s: SQL::Stmt = sql { // sql: { prog: () => Unit / { Splice[String], Splice[Int] } }: SQL::Stmt
do literal("SELECT * FROM Person p WHERE p.last_name = ")
do splice(name)
do literal(" AND p.age = ")
do splice(age)
} |
Fixes #722 and adds basic support for string templates.
There are a few caveats to keep in mind with the current initial implementation:
"${a.show} and ${b.show}"
instead ofa.show ++ " and " ++ b.show
wherea
andb
are some 'showable' values. Internally, however, this is just how it is desugared.show
on arguments which are not already of type string, the error messages are hideous and possibly confusing:error
Apart from that, the changes are really lightweight because of the early desugaring in the parser.
I am looking forward to your feedback and ideas.