Using .require for struct types

Erdem kerdemdemir at gmail.com
Tue Sep 13 06:17:54 UTC 2022


On Sunday, 11 September 2022 at 21:01:27 UTC, Salih Dincer wrote:
> On Saturday, 10 September 2022 at 16:33:03 UTC, Erdem Demir 
> wrote:
>> I wish I could use ref DListOfA returnVal = .... but we can't 
>> in D.
>>
>> Can you please suggest alternatives?
>
> I think you should try advanced update. Your flexibility and 
> what you can do are limited by your dreams. A couple delicious 
> code:
>
> ```d
> import object, std.container;
>
> struct A
> {
>     double val;
>     bool isBig;
> }
>
> void main()
> {
>     alias  DListOfA = DList!A;
>     DListOfA returnVal;
>     //DListOfA[string] temp;/*
>     DListOfA[string] temp = [
>         "a": DListOfA( A(0) )
>     ];//*/
>     
>     auto a = A(6, true); // replacement element
>     temp.update("a", {
>         return DListOfA( A(0) ); // not updated: unsucceeded 
> but initialized
>     }, (ref DListOfA v) {
>         v = DListOfA( a ); // existing v has been replaced
>         returnVal = v;
>         assert(returnVal.front == a);
>     });
>     assert(is(typeof(temp["a"]) == DList!A));
> }
> ```
> SDB at 79

I really like the possibilities of update as well but the 
overhead is too much especially in this example. I am sorry to 
say I found old "in" usage less complicated than that even I 
think "in" is complicated. I liked the Steven's solution all this 
code can be done via 2 lines only.




More information about the Digitalmars-d-learn mailing list