delete hash[key] deprecated???

Robert Fraser fraserofthenight at gmail.com
Mon Feb 11 20:23:37 PST 2008


Leandro Lucarella wrote:
> I have this example code:
> 
> class C { ... }
> 
> C[string] hash;
> 
> auto c = new C;
> hash["x"] = c;
> 
> // other place
> delete hash["x"];
> hash.remove("x");
> 
> And I got: Error: delete aa[key] deprecated, use aa.remove(key)
> 
> I don't want to remove the element of the array, I want to destroy the
> object stored at key "x". Does hash.remove("x") remove the item from the
> hash *and* delete c? I found that a little odd. If not, do I have to do:
> auto c = hash["x"];
> hash.remove["x"];
> delete c;
> 
> I find that a little odd too.
> 
> And yes, I want to explicitly delete the object because I want to minimize
> the GC activity since my program is kinda RT :)
> 
I see another, much bigger, problem with "delete hash[x]" meaning 
"hash.remove(x)" that goes against the D philosophy. One of the most 
important things about D is that it is generally unambiguously parsed 
without requiring semantic analysis. However, this is not so in the case 
of a DeleteExp, which can either actually refer to a delete expression 
or a function call. In other words, there are two semantic entities for 
things that are syntactically identical (in that case "delete 
[expression]"), and the effect of that depends not on the expression 
itself, but, in fact, only a piece of the expression.

Someone correct me if I'm misjudging this.


More information about the Digitalmars-d-learn mailing list