digitalmars.D.learn - Associative Arrays and Structs
Hi guys, I managed to perform a regression test on my use of associative arrays. Everything works up until you start trying to use operator overrides on an item within the associative array. For example, if I use Value[char[]], and Value has opCall(int), and I try to: Value[char[]] x = 3; That will give me an Error: Access Violation. But this works: Value x = 3; I'm assuming I need to do something else for this then, perhaps override opIndex() ?
Mar 17 2007
Value[char[]] x = 3;oops... bad example. I meant: Value[char[]] x; x["hello"] = 3;
Mar 17 2007
Dan wrote:For example, if I use Value[char[]], and Value has opCall(int), and I try to: Value[char[]] x = 3; That will give me an Error: Access Violation. But this works: Value x = 3; I'm assuming I need to do something else for this then, perhaps override opIndex() ?Not tested, but I think you need to do: Value[char[]] x; x["hello"] = Value.init; // or new Value() if Value is a class x["hello"] = 3; Or something similar. It seems to me that what happens is that x["hello"] = 3; is converted into x["hello"].opCall(3); but since the key "hello" doesn't exist in x, it fails. So you need to init the value first. -- Remove ".doesnotlike.spam" from the mail address.
Mar 17 2007