Double bracket "{{" for scoping static foreach is no longer part of D

ag0aep6g anonymous at example.com
Wed Dec 22 16:15:07 UTC 2021


On 22.12.21 17:01, rikki cattermole wrote:
> Anyway, AliasAssign has nothing to do with this. This "trick" creates a 
> closure aka ``() { ... }``. Thats all its doing.
> 
>  From the AST dump:
> 
> ```
> import object;
> import std;
> void main()
> {
>      {
>          string str = "Abc";
>          writeln("Hello D ", str, 2098L);
>      }
>      {
>          string str = "def";
>          writeln("Hello D ", str, 2098L);
>      }
>      return 0;
> }
> ```

In this context, `{ ... }` is not the same as `() { ... }`.

Also, `() { ... }` is not a closure, and does not necessarily involve a 
closure.

Just a scope:

----
import std.stdio;
void main()
{
     {
         string str = "Abc";
         writeln("Hello D ", str, 2098L);
     }
}
----

An immediately called function literal:

----
import std.stdio;
void main()
{
     () {
         string str = "Abc";
         writeln("Hello D ", str, 2098L);
     } ();
}
----

Returning a closure:

----
import std.stdio;
void main() { f("Abc")(); }
auto f(string str)
{
     return { writeln("Hello D ", str, 2098L); };
}
----


More information about the Digitalmars-d-learn mailing list