February 22, 2020 Dynamic array of Cycle buffers of size 2 which use a struct? | ||||
---|---|---|---|---|
| ||||
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); }); This give an error: Error: template object.require cannot deduce function from argument types !()(Cycle!(ms[2])[int], int, Cycle!(ms[2]) function() pure nothrow @nogc @system), candidates are: /Library/D/dmd/src/druntime/import/object.d(3225): require(K, V)(ref V[K] aa, K key, lazy V value = V.init) Not sure if the `m[2] t` survives the scope exit in form of a closure (?) or if cycle takes a reference to it... -- Robert M. Münch http://www.saphirion.com smarter | better | faster |
February 22, 2020 Re: Dynamic array of Cycle buffers of size 2 which use a struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | 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 |
Copyright © 1999-2021 by the D Language Foundation