Format specs idea
Georg Wrede
georg.wrede at iki.fi
Mon Apr 20 23:08:27 PDT 2009
Andrei Alexandrescu wrote:
> Nick Sabalausky wrote:
>> "BCS" <ao at pathlink.com> wrote
>>> Reply to Nick,
>>>
>>>> "Andrei Alexandrescu" <SeeWebsiteForEmail at erdani.org> wrote in message
>>>> news:gsiqdr$1csj$2 at digitalmars.com...
>>>>
>>>>> BCS wrote:
>>>>>> One option would be to not throw an error if the format string uses
>>>>>> indexing formats (e.i. out of order formatting)
>>>>>>
>>>>> Yah, that's an option I considered. Maybe it's the best way to go.
>>>>>
>>>> That would be far too clumbsy, unless you made it into two separate
>>>> functions.
>>>>
>>>> For instance (psuedocode):
>>>> auto userInput = getUserInput()
>>>> // userInput now contains "{Name} at {Address}", zip deliberately
>>>> ignored
>>>> writefln(userInput, name, address, zip); // They're used in-order, but
>>>> there
>>>> shouldn't be an error
>>> They are in order but are listed by name so the error doesn't throw.
>>> The case where the error would be thrown is where the only format
>>> strings used are the "get the next arg" kind.
>>>
>> I was just using names for illustrative purposes. Also, I was under
>> the impression that printf-style "get the next arg" formatting codes
>> were the only ones writef supported. Is this not so?
>>
> Since a few versions ago writef supports positional arguments with Posix
> syntax.
>
> Andrei
Positional format specifications really make a huge difference,
especially where one needs to have the user interface in several
languages. Thanks for them!!
Now, for the case without positional format specifications, we could
(and I guess, should) have a solution to "the wrong number of arguments"
problem. If we have:
- a non-printing format spec
- a munch-the-remaining-ones format spec
- an ignore-too-few-arguments format spec
we could cover all the needed cases.
The first one lets you use one parameter without printing it. (Maybe you
don't want to print the middle part of somebody's name.) The second is
used when you don't want to print possibly many remaining arguments, say
the address of someone, or the country.
The last one, is for the case where you use a single format string in
several contexts, some of which you suspect may not contain enough
information to be formally correct, but where you are satisfied to print
an empty string '' (that is, not print anything) for them.
This situation might arise when the format string originates from
outside the program.
** When none of the latter two are used, and there is the wrong number
of arguments, I think a runtime exception should be thrown.
The only time where I think a compile time error is warranted, is when
the format string is a string literal, with the wrong number of arguments.
The programmer may put the ignore-too-few-arguments format spec anywhere
in the format string. Its effect starts from that point on.
More information about the Digitalmars-d-announce
mailing list