Is this an acceptable hack work around for fmt producing a raw string literal? I'm a low intermediate web dev and I've only been playing around with nim for a couple of days so I am welcoming to harsh criticism and guidance.
# consider deprecating this, it's redundant with `fmt` and `fmt` is strictly
# more flexible, readable (no confusion with the binary `&`), self-documenting,
# not to mention #18275, bug #18278.
From reading this I get a sense that it would be a win to add escape character support to fmt. I might alter my question to ask why does & allow escape characters when fmt does not? What would need to be done to give that support to fmt?There's nothing special about fmt. This is just how Nim treats any occurrence of xyz"string" where there is no space between them.
See Raw string literals and the section "Generalised raw string literals" below it.
Here are some different ways of calling fmt and whether they interpret escape sequences or not:
echo fmt"hello\nworld" # generalized raw string literal - \n appears as-is
echo fmt("hello\nworld") # normal string literal - \n becomes newline
echo fmt "hello\nworld" # normal string literal - \n becomes newline
echo "hello\nworld".fmt # normal string literal - \n becomes newline
p.s. I think it's highly unlikely that & would be deprecated at this point, but even if it was you would have plenty of warning before it ever got removed.
The construct identifier"string literal" (without whitespace between the identifier and the opening quotation mark) is a generalized raw string literal. It is a shortcut for the construct identifier(r"string literal")
I see so echo"hello\n" also produces "hellon" in the console. echo fmt "hello {myVar}\n" & "bye\n{myVar}" works but the syntax highlighting is gone (not sure if this is a local issue for me). The best option for multi line strings using fmt then seems to be:
echo fmt("hello {myVar}\n") &
fmt("bye\n{myVar}")`
I see in the template for & in strformat there is a comment ## For a specification of the `& macro, see the module level documentation.` I'm having trouble finding that. My guess is that & allows for escape characters even without whitespace is because it is concatenating string literals, something inherent to the & operator?My guess is that & allows for escape characters even without whitespace is because it is concatenating string literals, something inherent to the & operator?
No, I believe the reason is simply that & is not an identifier, therefore the parser doesn't see &"foo" as a raw string literal. This is true for any unary operator, not just &.
see the module level documentation.
This is just referring to the description at the top of the file, nothing you haven't already seen. :)
Ahhh I see now. I attempted the following:
template `%`*(pattern: string{lit}): string {.callsite.} =
fmt(pattern, '{', '}', dummyForLineInfo)
and you're right, it behaves exactly like &. I think I will just use & as Calonger suggested lol.
Thanks so much for your responses! Nim seems like a lot of fun I look forward to learning more about it.
FWIW I stopped using strformat altogether (though & is much nicer than fmt IME) because no tool finds the bug in:
exec "gcc {a}"
vs
exec &"gcc {a}"
I think I have a better design but never got to write the RFC...