Dynamic array of Cycle buffers of size 2 which use a struct?

Ali Çehreli acehreli at yahoo.com
Sun Feb 23 00:17:29 UTC 2020


On 2/22/20 2:29 PM, Robert M. Münch wrote:
> I don't get how I can create a dynamic array of Cycle buffers of size 2 
> which use a struct.
> 
>   struct ms {
>     int a;
>     int b;
>   }
>   ms[2] msBuffer;
>   alias circularStructBuffersT = typeof(cycle(msBuffer));
>   circularStructBuffersT[int] circularStructBuffers;
> 
>   int i = 2;
>   auto x = circularStructBuffers.require(i, (){
>     ms[2] t;
>     return cycle(t,2);
>   });

It looks like require() requires :) a value, which it evaluates only if 
necessary:

   https://dlang.org/spec/hash-map.html#inserting_if_not_present

In your case, you're passing a callable. All you need to do is to 
actually call that callable with empty parenthesis:

   auto x = circularStructBuffers.require(i, {
     // ...
     }());  // <-- HERE

Because the parameter is lazy, it will still be called only when needed.

Ali



More information about the Digitalmars-d-learn mailing list