Wanted: Format character for source code literal
Berni44
someone at somemail.com
Tue May 4 07:54:19 UTC 2021
On Monday, 3 May 2021 at 23:08:55 UTC, Q. Schroll wrote:
> Please don't do this. Format characters can be customized. Any
> character you'd introduce for it either wouldn't work for some
> types or break those types' formatting.
I think, this doesn't hurt: The call of a `toString` has
precedence for compound types like structs and classes (and in
most of these cases it won't be possible to add a generic literal
at all, see my post above). So, if you use some of the predefined
qualifiers, the customized version will always be used, even if
it has a completely different meaning. (Admittedly it might cause
some confusion, if the customized versions are not well
documented.)
> Why not introduce a new function like `dlangLiteral` that takes
> the value and returns a string? It can be used in `format`
> quite easily (like `format("pre %s post",
> dlangLiteral(<something>));`) and is explicit and not a special
> case at all.
In my opinion, the main idea behind this formatting routines is,
to have a simple and short way for formatting output. We could
use your idea for every other format character too, like:
`format("%s = %s", character('𝜋'),
scientificFloatingPoint(3.14))`. We don't do that, because it's
more convenient to write `format("%c = %e", '𝜋', 3.14)`.
Furthermore, there are all the parameters, that can be applied:
`format("%-3c = %+.4e", '𝜋', 3.14)` is a simple way to change the
formatting. Without it it would become something like `format("%s
= %s", character!(true, false, false, false, false, false,
3)('𝜋'), scientificFloatingPoint!(false, true, false, false,
false, false, FormatSpec.UNSPECIFIED, 4)(3.14))`.
An other problem will be, when used with arrays, ranges and the
like, e.g. you can do something like `format("val = [%(%D,\n
%)];", my_array);` to get an output with each value on a
separate line. Without this literal you would at least need to
map `my_array` using `dlangLiteral` and in generic code this
might even cause more trouble.
More information about the Digitalmars-d
mailing list