I was just reading the BoxLang docs and noticed multi-line strings support Quick Syntax Style Guide | BoxLang : A Modern Dynamic JVM Language
myLargeContent = """
Hi,
How are you today #name#!
This is a very nice email with merged data!
Thanks for purchasing #item#!
"""
How is that different to just doing:
myLargeContent = "
Hi,
How are you today #name#!
This is a very nice email with merged data!
Thanks for purchasing #item#!
"
Might have found a bug…
foo.bxs
myLargeContent = "
Hi,
How are you today!
This is a very nice email with merged data!
Thanks for purchasing
"
echo( "<pre>#myLargeContent#</pre>" )
echo( "<pre>-------------</pre>" )
myLargeContent = """
Hi,
How are you today!
This is a very nice email with merged data!
Thanks for purchasing
"""
echo( "<pre>#myLargeContent#</pre>" )
echo( "<pre>-------------</pre>" )
The output is:
Hi,
How are you today!
This is a very nice email with merged data!
Thanks for purchasing
-------------
"
Hi,
How are you today!
This is a very nice email with merged data!
Thanks for purchasing
"
-------------
I wasn’t expecting the "
to show in the 2nd one.
That indicates it’s a regular string, split across multiple lines, and the two ""
inside the string just became escaped double-quotes, and thus printed as "
.
The idea behind multi-line strings is the triple quotes allow for embedded quotes to not be escaped, so the following should work:
multiLine = """
Sean says "This should work!"
"""
whereas with a regular string you’d need to do this:
regularString = "
Sean says ""This should work!""
"
1 Like
Ah OK. Thank you. Yes - makes sense. That’s useful to know.
I was reading up a bit on how it works in other languages and it seems that Java strips out the indentation? So:
String myMultiLineText = """
first line
second line
""";
produces:
first line
second line
Whereas BoxLang seems to keep the indentation whitespace. I’m not sure if that’s useful or confusing (a bit of both most likely!). Groovy seems to have a stripIndent()
I recommend @bdw429s or whoever is spec-ing this out considers this: trimIndent - Kotlin Programming Language
Possibly better than monkeying with string indentation automagically.
1 Like
Just tried that and get:
The requested key [This] was not located in any scope or it’s undefined
Full boxlang.bxs
code:
myLargeContent = """
Sean says "This should work!"
"""
echo( "<pre>#myLargeContent#</pre>" )
I’m not sure how that got in the docs, as the parser has no such support for this. Luis may have thrown around the idea early on, but we never really discussed it.
I’m familiar with this because were using that feature all the time in our Java source code. I’ll say, I’m not sure I’m convinced of the feature in BL tho. CF/BL already allow for easy mutliline strings, and the mix of single and double quotes (which Java doesn’t have) allow you to match the contents of the string to hopefully not need to escape. CF/BL’s ability to just embed line break literals is also something Java doesn’t allow. I would actually question if they should even interpolate by default, or if that would go against the idea of NOT needing to escape things inside the multi-line string.
The main benifit would be stripping whitespace then, which isnt a huge deal. I know dealing with strings used to be a pain in Java, but if we copied this feature, I’m not clear on whether it would be bringing something tangible to the table and solving actual problems that affect us, or just plucking Java features because they look cool.
1 Like
Well that would explain why I can’t get it to work then I will open a PR on the docs to remove it as it’s pretty much one of the first code examples as you read in the gitbook!
Well, as long as you don’t have both "
and '
in the same string
2 Likes