Time to kill T() as (sometimes) working T.init alias ?
Dan
dbdavidson at yahoo.com
Mon Dec 10 10:40:32 PST 2012
On Monday, 10 December 2012 at 11:03:28 UTC, Walter Bright wrote:
>
> Have you ever written a struct that requires a deep copy?
>
> I have, and I always wound up redoing it so the deep copy was
> unnecessary.
How do you get around it? Let's say you have an address book
structure that organizes addresses by email. Without a postblit
or some deep copy, duplicating means sharing. When I see this, I
think - I better add a postblit in case address books get copied.
How would you approach it to not need deep copy? For something as
simple as this would you introduce COW?
Thanks,
Dan
---------------------------
module phone.phone;
import std.stdio;
struct Address {
string street;
string zipCode;
string state;
string country;
}
struct AddressBook {
alias Address[string] EmailAddressMap;
void addAddress(string email, Address address) {
_emailAddressMap[email] = address;
}
private {
EmailAddressMap _emailAddressMap;
}
}
unittest {
AddressBook office;
office.addAddress("foo at aol.com", Address("201 Foo Dr", "99999",
"Fl", "USA"));
AddressBook copy = office;
copy.addAddress("goo at aol.com", Address("202 Foo Dr", "99999",
"Fl", "USA"));
writeln(office);
writeln(copy);
}
More information about the Digitalmars-d
mailing list