digitalmars.D.learn - Using .require for struct types
- Erdem Demir (24/24) Sep 10 2022 import std;
- Steven Schveighoffer (7/9) Sep 10 2022 Use a pointer.
- Erdem (3/12) Sep 12 2022 Actually that could be answer I am seeking for I will try it.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (14/16) Sep 10 2022 But keeping a reference to a temporary would not work because the life
- Erdem (6/24) Sep 12 2022 In the code sample I posted I need returnVal.insert(a); have an
- Salih Dincer (32/35) Sep 11 2022 I think you should try advanced update. Your flexibility and what
- Erdem (6/44) Sep 12 2022 I really like the possibilities of update as well but the
import std; struct A { double val; bool isBig; } void main() { alias DListOfA = DList!A; DListOfA[string] temp; A a = {2.0, true}; DListOfA returnVal = temp.require("a", DListOfA());--> I wish I could use ref DListOfA here returnVal.insert(a); writeln(temp); } On the reference page there is an example with class types only (https://dlang.org/spec/hash-map.html) but my type in associative array is a DList which is a struct type and that causes I am having a copy of my DList after require an operation. And of course my insert is happening to the copy and is not affecting to the associative array. , I wish I could use ref DListOfA returnVal = .... but we can't in D. Can you please suggest alternatives?
Sep 10 2022
On 9/10/22 12:33 PM, Erdem Demir wrote:Can you please suggest alternatives?Use a pointer. ```d DListOfA *returnVal = &temp.require(...); returnVal.insert(a); ``` -Steve
Sep 10 2022
On Saturday, 10 September 2022 at 18:39:55 UTC, Steven Schveighoffer wrote:On 9/10/22 12:33 PM, Erdem Demir wrote:Actually that could be answer I am seeking for I will try it.Can you please suggest alternatives?Use a pointer. ```d DListOfA *returnVal = &temp.require(...); returnVal.insert(a); ``` -Steve
Sep 12 2022
On 9/10/22 09:33, Erdem Demir wrote:DListOfA returnVal = temp.require("a", DListOfA());--> I wish I could use ref DListOfA hereBut keeping a reference to a temporary would not work because the life of that temporary ends by the end of that expression (practically, at the semicolon). An option is to allocate the object dynamically with new (and store DListOfA* in the associative array). Then the GC would keep it alive as long its pointer was in the associative arrray. But a better option is to just forget about it because D already takes care of rvalues by blitting (bit-level copying) them by default. Everything just works... :) It is not expensive either. For example, your struct is very cheap to copy. But I am probably missing the reason why you want a ref there. Perhaps there are even better options. Ali
Sep 10 2022
On Saturday, 10 September 2022 at 18:38:40 UTC, Ali Çehreli wrote:On 9/10/22 09:33, Erdem Demir wrote:In the code sample I posted I need returnVal.insert(a); have an effect on DListOfA[string] temp; but if returnVal is a rvalue copy I can't accomplish this right? I see your point about ref would have been assign to a temp val, thanks for pointing out.DListOfA returnVal = temp.require("a",DListOfA());--> I wish Icould use ref DListOfA hereBut keeping a reference to a temporary would not work because the life of that temporary ends by the end of that expression (practically, at the semicolon). An option is to allocate the object dynamically with new (and store DListOfA* in the associative array). Then the GC would keep it alive as long its pointer was in the associative arrray. But a better option is to just forget about it because D already takes care of rvalues by blitting (bit-level copying) them by default. Everything just works... :) It is not expensive either. For example, your struct is very cheap to copy. But I am probably missing the reason why you want a ref there. Perhaps there are even better options. Ali
Sep 12 2022
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 79
Sep 11 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 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.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 79
Sep 12 2022