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
Multi-dollar interpolation #375
Comments
Won't this change the meaning of code like this: // 1)
// prints "foo" now
// would print foo in the future, the literal is starting with 4 "
println(""""foo"""")
// 2)
// prints "foo now
// would fail to compile in the future
println(""""foo""") I have written code like 2) recently to create single line JSON literals: val json = """{"id":"0","guild_id":"0","name":"rule","creator_id":"0","event_type":1,"trigger_type":3,""" +
""""trigger_metadata":{},"actions":[],"enabled":false,"exempt_roles":[],"exempt_channels":[]}"""
// ^^^^
// notice the 4 " here |
I understood the proposal to imply that the number of leading/trailing |
also curious, in this example: $$"$${order.product} costs $ $${order.price}" will removing space after |
It will print
|
It won't, as we'd like to have backwards-compatible rules and require the dollar sign for the new rules. However, this approach implicitly introduces a new kind of string literals, and that is a pretty high price for this issue. We'll easily run into subtle differences because the
And there are a lot of single-line strings that start with more than three |
Indeed, the dollar escaping rule looks good to me, but the subtle difference between two versions of A string literal, whether single line or multi-line, when starting with N
|
$
and "
in string literals
I've pushed a new version of the proposal, removing everything related to double quotes. That way the KEEP is only about multi-dollar interpolation. Thanks @lukellmann for noticing the problems with the proposed approach so fast. 🚀 |
I think the case of embedding |
But if we don't solve the triple double quotes problem, then the whole proposal is only a partial solution, and it wastes the syntax space. Nowadays more and more languages are using triple double quotes, and we will have more and more chance to encounter the need to embed them. When we regret today's decision, we will have to introduce even more syntax to solve it. I am not requesting to solve every unknown problems, but at least please solve the problems we do know now. |
How is adding just a trailing sequence of Is "short" better than "good"? I truly hope not. |
Sorry, I didn't mean that we shouldn't look for the best solution, and certainly using words like "ceremony" wasn't good on my side. My apologies. The problem statement, as I see it, is the following. We would like to find a way to include Can we imagine another such solution? In particular, @Peanuuutz, does your proposed solution satisfy these requirements? |
After I tested a few cases, the answer is no. There's only one very edge case where my proposal will fail.
That is, now I can't have Please note that, even though this happens, we can't allow the surrounding quotes to grow as it then falls into the original situation where quotes are gone after a Actually, after I read the original ticket, I'm more in favor of a new representation -
|
Firstly, thx serras for writing all those KEEPs 🙏 Secondly, I find the solution to the problem very nice, esp. because I wished for it to have in Kotlin as I saw it being added to C# 11. Concerning string, well... as I write Rust, there is no "multiline" strings, as the strings can accept newlines like that anyway. So there is only |
Trimming the indent for multi-line strings at compile time sounds attractive. Why add bloat and bear the cost of invoking |
|
I do think there may be value in having a way to "compile-time trim this multi-line string, but not the expressions interpolated into it", but we don't have a way to express that now. |
As discussed at the beginning of the KEEP, we've decided to split this concern to another KEEP which is in the works. The reason why trimming is harder is because of possible interactions with string templates. |
@serras did you see the C# trimming for """ strings? What's your take on that? |
Yes, I've seen how they did it. However, whatever solution we come up with, we need to be cautious not to break backwards compatibility, and trying to get some uniformity across the language. When our team discussed the issue, we came to two conclusions:
As hinted above and by this message, we are working on this. However, it may take longer to reach a solution. |
After some discussion, we've decided not to handle the problem of three double quotes in a multiline string.
Our preliminary code search for usages of In contrast, code search for usages of |
Do I understand correctly that the case for using a single-dollar prefix If so, wouldn't it be reasonable to change the proposed syntax so that a prefix now requires 2 or more consecutive |
This KEEP proposes multi-dollar interpolation. The current full text of the proposal can be found here.
We propose an extension of string literal syntax to improve the situation around
$
in string literals. Literals may configure the amount of$
characters required for interpolation.The text was updated successfully, but these errors were encountered: