Closed al6x closed 3 years ago
Making string interpolation a part of a language is strictly inferior solution on my opinion, as it requires hardcoding a particular behavior that can easily be implemented using reader macro.
Current implementation does not support using if/else only because it is implemented using rather straightforward expression parser https://github.com/nim-lang/Nim/blob/f25243140baa00db4a663d453a262e41f8d9073f/lib/pure/strformat.nim#L564 it would be very annoying to have to reimplement parser for "any expression", but if/else
is common enough case, and quite simple to fix.
only stared at this for five minutes, but the fact that ':' is used in the DSL as the format specifier delimiter seems to be the main issue.
adding fmtChar=':' to
strformatImpland
fmt`, would at least allow users to override that delimiter and use for loops and if statements. it's not perfect but at least it wouldn't break anything.
The format specifier delimiter was taken from Python fwiw. But I haven't checked if Python allows for other constructs that also use the colon (slicing comes to mind).
Python can handle colons in slices. I think they rely on colons being wrapped in [ ... ]
though, i.e. parsing may be handled similarly to colon in quotes. For instance declaring a lambda doesn't work:
This is now supported but colons have to be inside any kind of parenthesis.
Awesome! Thanks guys! :)
Currently
fmt
wont' work with object creation, if/else and other limitations.It's possible to workaround those problems, as in example below, but would be nice if
fmt
would support any expression. Maybe even make string interpolation part of language as it's used very frequently.