DIP 1027--String Interpolation--Final Review Discussion Thread
Adam D. Ruppe
destructionator at gmail.com
Mon Feb 3 12:55:15 UTC 2020
On Monday, 3 February 2020 at 05:10:13 UTC, Arine wrote:
> Part of the whole reason is that this works with printf(), the
> examples in the DIP almost exclusive use the C functions. Why
> stop short of custom types?
This DIP doesn't format them at all. It is fundamentally just a
syntax sugar rewrite of a string into an argument list.
> Implement string_is_ok() for me.
That varies based on what features you choose to support. It
could be as simple as assert(string.count("%s") == args.length)
and you ignore anything else. Or you could go crazy with all
kinds of custom specifiers. The compiler doesn't do anything
except
1) replace $xxxx with %s in the string
2) blindly copy/paste what the user said ${xxxx} while moving the
var to the argument list
> Ensuring that it correctly interprets all of printf's features.
This is not necessary. You can make your version only do the bare
minimum.
> You can respond to arbitrary formatting, but what does the
> compiler choose to insert? The DIP makes no mention of this.
The compiler ONLY uses %s unless the string's author specifies
something else in the string itself. Anything else is up to the
user - and the user is supposed to look up what the function they
are calling supports. That's what this paragraph from the dip is
about:
===
The {%d} syntax is for circumstances when the format specifier
needs to be anything other than %s, which is the default. What
goes between the { } is not specified, so this capability can
work with future format specification improvements without
needing to update the core language. It also makes interpolated
strings agnostic about what the format specifications are.
===
Unless the person calling your function SPECIFICALLY writes
something else - which you can say in the docs "do not attempt".
> Just calling toString() for the C functions isn't sufficient.
and the dip doesn't do that. That's why it passes the arguments
unmodified - it is up to the function you are calling to do
whatever it needs to do.
Really, the C compatibility thing is a red herring. This dip is
NOT actually compatible with printf out of the box; it silently
does the wrong thing in almost ALL cases.
What it does do is provide a hook for users that can be
compatible with it. It does extremely little on its own.
More information about the Digitalmars-d
mailing list