On Thursday, 31 March 2016 at 13:39:25 UTC, ixid wrote:
What is going on with UFCS and foreach?

foreach(i;0..5).writeln;

This is not UFCS; it is calling writeln on module scope. See http://dlang.org/spec/module.html#module_scope_operators

Your code is semantically identical with

foreach (i; 0 .. 5)
{
    .writeln; // module scope operator!
}

Furthermre because there is no local variable writeln that could be confused with the imported function, the code is identical with

foreach (i; 0 .. 5)
{
    writeln;
}

This prints five line breaks.

foreach(i;0..5).i.writeln;

This will not compile.

It does not compile because i is not an identifier at module scope. i is a local variable and a hypothetical i at module scope (accessible via .i) is not declared, therefore this is an error.

foreach(i;0..5).writeln(i);

This writes out 1 to 4 on separate lines. Is this supposed to work? I thought a.b would be rewritten to b(a) with UFCS but writeln(foreach(i;0..5)) is nonsensical and does not compile and should be the same as foreach(i;0..5).writeln;

It does rewrite. But only if there is an expression beforehand the dot. Here it just isn't. The "foreach (i; 0 .. 5)" is not a complete expression.

Reply via email to