why doesn't this compile : `val one = """text \$te...
# announcements
t
why doesn't this compile :
val one = """text \$text"""
it won't escape the $. Works fine in a normal string. how do I turn of templating in raw strings?
z
“””${‘$’}”””
🤣 1
t
brutal....
r
To answer why, it's because it's a raw string. To quote the docs (https://kotlinlang.org/docs/basic-types.html#string-literals)
A raw string is delimited by a triple quote (
"""
), contains no escaping and can contain newlines and any other characters
t
not really much of a "raw string" if it's got templating in it.... is it?
r
I guess that's up to how you understand "raw". It doesn't really have a universal definition (at least not that I'm aware of). It's just the term they decided to use for this kind of string.
A common use case for raw strings besides multiline is regex, where not having to deal with double escaping everywhere is quite nice.
t
"...contains no escaping..." oh.. except the '$' charactor.
I use $ in regex all the time. but I use normal strings, in which case \$ works fine.
it's fine. ${'$'} works. I just wish it was clearly documented someplace.
e
it is documented, right there in the language documentation?
t
really? It just says the bit about "...contais no escaping.. " but templating works. It doesn't say how to escape the templating. It does say how for normal string. \$. but not ${'$'} for raw strings.
r
That's not escaping, that's interpolation, but I do see where you're coming from. A lot of the decisions made for Kotlin were around how practical or common the use case, not necessarily how well it fits an existing definition.
While I also use
$
in regex, I definitely don't use it anywhere near as much as
\
.
t
true
maybe they'll make a "really actually raw string". Although it's really only useful for code generation... and there are libs for that.
😆 1
r
Maybe the current ones should have been called "medium rare" strings 🙂
😂 2
e
e.g. kotlinpoet already knows how to emit literal strings with whatever content you desire
heh. reminds me of terminal modes: raw (unprocessed), cooked (processed), and rare (somewhere in between)
t
I used kotlinpoet a lot. This problem came up because I have a base class all the kotlinPoet code extends. But I keep it as a string because I'm not sure of the eventual package name...o
y
I mostly use
${"$"}
in this case because it looks neat enough
p