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

There's no such thing as UFCS on foreach. UFCS only works on variables, not a foreach statement.

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

You can add a line break and optional braces there to see what it really is:

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


It is just an ordinary loop calling writeln 5 times. The leading dot in D means "look up in the global namespace".

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

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

It is trying to look up a name i in global scope, and calling writeln on it.

This is why the .name syntax exists: so you can bypass local variables with the same name when trying to access a global.

It would compile if you put an `int i;` at the top of your module... try it!


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

foreach(i; 0 .. 5) {
   .writeln(i); // leading . just means call global writeln
}




What you're seeing is similar to the Java hoax where people say it supports hyperlinking:

public static void main() {
   http://dlang.org/awesome
}


That compiles, but it isn't a link, it just looks like one at first.

It is actually a label, "http:", followed by a comment, "//dlang.org....". Two legal things without whitespace that looks like something else at first glance.

Reply via email to