digitalmars.D.learn - An struct copy constructor that can cope with an AA?
- mark (31/31) Mar 09 2020 I have this struct:
- Steven Schveighoffer (7/47) Mar 09 2020 aa.dup. However, this isn't going to work in a const member function
- mark (9/15) Mar 09 2020 [snip]
- Steven Schveighoffer (8/26) Mar 09 2020 Right, because it's inside a const member function, the tags member is
I have this struct: struct Deb { string name; ... Unit[string] tags; // set of tags Deb copy() const { Deb deb; ... foreach (key, value; tags) // XXX deb.tags[key] = value; return deb; } void clear() { name = ""; ... tags.clear; } ... } I am populating an AA with these structs: Deb[string] debForName; I'm using this approach (pseudo-code): Deb deb; foreach (datum; data) { populateDeb(datum, deb); debForName[deb.name] = deb.copy; // YYY deb.clear; } (1) XXX Is there a nicer way to copy an AA? (2) YYY Is there a nicer way to copy a struct? Use a copy constructor or implement a dup or idup?
Mar 09 2020
On 3/9/20 9:23 AM, mark wrote:I have this struct: struct Deb { string name; ... Unit[string] tags; // set of tags Deb copy() const { Deb deb; ... foreach (key, value; tags) // XXX deb.tags[key] = value; return deb; } void clear() { name = ""; ... tags.clear; } .... } I am populating an AA with these structs: Deb[string] debForName; I'm using this approach (pseudo-code): Deb deb; foreach (datum; data) { populateDeb(datum, deb); debForName[deb.name] = deb.copy; // YYY deb.clear; } (1) XXX Is there a nicer way to copy an AA?aa.dup. However, this isn't going to work in a const member function (though technically there shouldn't be anything wrong with it).(2) YYY Is there a nicer way to copy a struct? Use a copy constructor or implement a dup or idup?I would name it dup instead of copy for consistency with D. A copy constructor is pretty heavy for a struct to do a complete duplication of the AA. You should have to opt-in to that. -Steve
Mar 09 2020
On Monday, 9 March 2020 at 14:45:15 UTC, Steven Schveighoffer wrote:On 3/9/20 9:23 AM, mark wrote:[snip]I have this struct:I would name it dup instead of copy for consistency with D. A copy constructor is pretty heavy for a struct to do a complete duplication of the AA. You should have to opt-in to that. -SteveThanks, I'd already realised I ought to rename it .dup. Also, I only actually need to create it from the keys since I'm using the tags AA as a set so every value is the same. I did try tags.dup but got this: src/deb.d(25,24): Error: cannot implicitly convert expression dup(this.tags) of type const(void[0])[string] to void[0][string]
Mar 09 2020
On 3/9/20 11:49 AM, mark wrote:On Monday, 9 March 2020 at 14:45:15 UTC, Steven Schveighoffer wrote:Right, because it's inside a const member function, the tags member is const. Right now, it looks like dup does not work even for value types (e.g. const int[int] -> int[int]) let alone types with indirection (it should also work for string keys). If there's not an issue already, someone should file it. -SteveOn 3/9/20 9:23 AM, mark wrote:[snip]I have this struct:I would name it dup instead of copy for consistency with D. A copy constructor is pretty heavy for a struct to do a complete duplication of the AA. You should have to opt-in to that.Thanks, I'd already realised I ought to rename it .dup. Also, I only actually need to create it from the keys since I'm using the tags AA as a set so every value is the same. I did try tags.dup but got this: src/deb.d(25,24): Error: cannot implicitly convert expression dup(this.tags) of type const(void[0])[string] to void[0][string]
Mar 09 2020