www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does `static foreach` lead to something calling `~=` internally?

reply 0xEAB <desisma heidel.beer> writes:
Why does this `static foreach` lead to hidden usage of operator 
`~=` calls in some cases?

 static foreach(i; 0 .. cnt)
 onlineapp.d(9): Error: cannot use operator ~= in  nogc delegate 
 onlineapp.xv!(myUDA("/")).__funcliteral2.__lambda1
 import std.traits;
 
 private  safe pure nothrow  nogc
 {
     enum xv(alias uda) = function()
     {
         enum cnt = 10;
 
         static foreach(i; 0 .. cnt)
         {
         }
     };
 }
 
 struct myUDA
 {
     string path;
 }
 
  myUDA("/")
 void doSth(string path)
 {
 }
 
 extern(C) void main()
 {
     xv!(getUDAs!(doSth, myUDA))();
 }
Try: https://run.dlang.io/is/clQgeA
Jul 07 2019
next sibling parent reply a11e99z <black80 bk.ru> writes:
On Sunday, 7 July 2019 at 16:51:57 UTC, 0xEAB wrote:
 Why does this `static foreach` lead to hidden usage of operator 
 `~=` calls in some cases?
probably same https://forum.dlang.org/post/qd9ee0$2eud$1 digitalmars.com
Jul 07 2019
parent a11e99z <black80 bk.ru> writes:
On Sunday, 7 July 2019 at 17:07:59 UTC, a11e99z wrote:
 On Sunday, 7 July 2019 at 16:51:57 UTC, 0xEAB wrote:
 Why does this `static foreach` lead to hidden usage of 
 operator `~=` calls in some cases?
probably same
oops! this one https://forum.dlang.org/post/eidpgqohllwmuumxwrbf forum.dlang.org
Jul 07 2019
prev sibling parent reply 0xEAB <desisma heidel.beer> writes:
On Sunday, 7 July 2019 at 16:51:57 UTC, 0xEAB wrote:
 Why does this `static foreach` lead to hidden usage of operator
Further notes by Dan (aka "Wild"):
 I added some small printfs to the compiler, http://ix.io/1NWM
 It seems like it lowers it into something weird
Jul 07 2019
parent Exil <Exil gmall.com> writes:
On Sunday, 7 July 2019 at 18:45:14 UTC, 0xEAB wrote:
 On Sunday, 7 July 2019 at 16:51:57 UTC, 0xEAB wrote:
 Why does this `static foreach` lead to hidden usage of operator
Further notes by Dan (aka "Wild"):
 I added some small printfs to the compiler, http://ix.io/1NWM
 It seems like it lowers it into something weird
Static foreach needs a tuple. When you have a range like "0 .. 10" it has to convert that to a tuple. It does this by generating some code and running it at CTFE. Guess this was done as it was a lot simpler to do, you don't have to worry about recreating ranges as that's already done by using CTFE. What it ends up lowering to: https://github.com/dlang/dmd/blob/v2.087.0/src/dmd/cond.d#L274 * static foreach (x; { * typeof({ * foreach (x; range) return x; * }())[] __res; * foreach (x; range) __res ~= x; * return __res; * }()) { ... }
Jul 09 2019