Orphan format arguments: args[0..1]

Ali Çehreli acehreli at yahoo.com
Sun Dec 16 00:34:48 UTC 2018


This one confused me until I decided to talk to a rubber ducky:

import std.string;

void main() {
     auto s = "%s is a good number".format(42);
}

Fine; it works... Then the string becomes too long and I split it:

     auto s = "%s is a good number but one needs to know" ~
              " what the question exactly was.".format(42);

Now there is a compilation error:

   Orphan format arguments: args[0..1]

What? Is that a bug in format? It can't be because the string should be 
concatenated by the compiler as a single string, no? No: operator dot 
has precedence over ~, so format is applied to the second part of the 
string before the concatenation. Doh! This puzzled me a lot.

Anyway, the solution, once again, is to use parentheses:

     auto s = ("%s is a good number but one needs to know" ~
               " what the question exactly was.").format(42);

Ali


More information about the Digitalmars-d-learn mailing list