This is really a pre-announcement, just something I whipped up this
afternoon, that I've added to my StringUtils.jl package,
as a WIP PR: https://github.com/ScottPJones/StringUtils.jl/pull/1
I've pulled in the code from Tom Breloff's very nice PR #10 on
JuliaLang/Formatting.jl, so that now, in addition to being able to handle
Swift style syntax for string literals, for hex, Unicode, and string
interpolation, along with my own additions of emoji and LaTex character
literals,
this PR adds formatting that takes setting defaults based on the type (and
it's extensible, all thanks to Tom's work), as well as handling more C style
formatting.
Some advantages are over the @*printf macros: the formatting is done right
next to the values passed, not at some position among many
variables passed in, and I believe that it doesn't require as much JIT
generated code.
Here are some examples of using it:
*twocents = .02 ; println(u"Here are my $\%(twocents,4,2) worth")*
Here are my $0.02 worth
*julia> **twocents = .02 ; println(u"Here are my $\%4.2f(twocents) worth")*
Here are my $0.02 worth
*julia> **twocents = .02 ; println(u"I will drive a \N{dagger} through
Dracula's \:heart:")*
I will drive a † through Dracula's ❤
*julia> **ptr = pointer([1,2,3]) ; println(u"Here is a pointer:
0x\%016x(ptr)")*
Here is a pointer: 0x000000010b766ce0
*julia> **fmt_default!(Float64; width=4, prec=2)*
*julia> **u"\%(twocents)"*
*"0.02"*
Some advantages over normal Julia string literals: you don't have to worry
about the $ character needing to be quoted, when dealing with LaTex
strings, monetary amounts, or when dealing with strings from other
languages, where it doesn't need to be quoted.
Also, you don't have to worry if you might be using a character that could
be taken as part of a literal (which can change in the future, as more
Unicode characters are added to Julia's identifier character table).
I helped somebody out with this just today, where they tried to do
greet = "Hello" ; whom = "World" ; println("$greet, $whom!")
and got an error, because they hadn't realized that the ! would be taken as
part of the identifier.
There is still work to be done to make sure that the C-style format
specifications are what people would expect, and that the `fmt` style
(which can take keyword arguments),
has all the options that are present in Dahua Lin's Formatting package.
If the Python style of formatting can be added in a clean way, I'll try to
add that as well.
I picked the following syntax for having LaTex symbols:
"\{latexname}", but maybe people have some other ideas? (my design
requirements here are that 1) it must start with a \ and 2) have some
character or pair of characters to delimit the name, i.e. "\$latexname$" or
"\|latexname|" or "\[latexname]")
I hope people will find some use from this, and give feedback on how to
improve it.
Thanks again to the core team and all the contributors for a language that
allows me to customize it in interesting ways so easily!
-Scott