www.digitalmars.com         C & C++   DMDScript  

D - delete on array slots and objects

reply Hauke Duden <Hauke_member pathlink.com> writes:
I am a little concerned about the way the delete operator is used with arrays.

The documentation states that

"""
Particular keys in an associative array can be removed with the delete operator:


delete b["hello"];

This confusingly appears to delete the value of b["hello"], but does not, it
removes the key "hello" from the associative array.
"""

So how can one delete the object that the array slot "hello" points to? Does one
have to use a temporary variable to store the pointer/reference and then call
delete on that?

If so, it would mean that

SomeClass o=b["hello"]
delete o;

and

delete b["hello"]

wouldn't be the same. This is inconsistent with the way expressions and
temporary variables are usually handled in C/C++ and also D.

int i=3+4;
int x=i+5;

yields the same result as

x=(3+4)+5;

I think this is pretty messy (and looking at the last sentence the author also
seems to be aware of that). Also, as I understand it, one of the design goals of
D is that code that looks the same as C/C++ code also behaves the same (see the
switch statement). In C++ "delete b[i]" would delete the object in b[i], not the
slot i of array b.

I think the main problem is that the delete operator is used for two different
things at the same time. Maybe a cleaner solution would be to have a dedicated
operator (or array "member function") to delete slots of an array.

Then one could write something like this in D:

deleteslot b["hello"];

and the ambiguity would be gone.

I have no idea wether it is still early enough to make such a change (I only
recently stumbled upon D), but I think it would be a worthwhile thing to
consider to ensure consistency within the language.
Sep 13 2003
next sibling parent Mike Wynn <mike l8night.co.uk> writes:
Hauke Duden wrote:
 I am a little concerned about the way the delete operator is used with arrays.
 
 The documentation states that
 
 """
 Particular keys in an associative array can be removed with the delete
operator:
 
 
 delete b["hello"];
 
 This confusingly appears to delete the value of b["hello"], but does not, it
 removes the key "hello" from the associative array.
 """
 
 So how can one delete the object that the array slot "hello" points to? Does
one
 have to use a temporary variable to store the pointer/reference and then call
 delete on that?
 
 If so, it would mean that
 
 SomeClass o=b["hello"]
 delete o;
 
 and
 
 delete b["hello"]
 
 wouldn't be the same. This is inconsistent with the way expressions and
 temporary variables are usually handled in C/C++ and also D.
 
 int i=3+4;
 int x=i+5;
 
 yields the same result as
 
 x=(3+4)+5;
 
 I think this is pretty messy (and looking at the last sentence the author also
 seems to be aware of that). Also, as I understand it, one of the design goals
of
 D is that code that looks the same as C/C++ code also behaves the same (see the
 switch statement). In C++ "delete b[i]" would delete the object in b[i], not
the
 slot i of array b.
 
 I think the main problem is that the delete operator is used for two different
 things at the same time. Maybe a cleaner solution would be to have a dedicated
 operator (or array "member function") to delete slots of an array.
 
 Then one could write something like this in D:
 
 deleteslot b["hello"];
 
 and the ambiguity would be gone.
 
 I have no idea wether it is still early enough to make such a change (I only
 recently stumbled upon D), but I think it would be a worthwhile thing to
 consider to ensure consistency within the language.
 
deleteslot is almost as confusing as delete, why not Object[wchar[]] hashtbl; ... hashtbl.remove( "foo" ); or hashtbl.remove["foo"]; or will other objects have `manipulators` in the form <name> object '[' <params> ']' like the perl where restore $foo a,b,c; => $foo->restore( a, b, c );
Sep 13 2003
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Hauke Duden" <Hauke_member pathlink.com> wrote in message
news:bjuudk$nth$1 digitaldaemon.com...
 I am a little concerned about the way the delete operator is used with
arrays.
 The documentation states that

 """
 Particular keys in an associative array can be removed with the delete
operator:
 delete b["hello"];

 This confusingly appears to delete the value of b["hello"], but does not,
it
 removes the key "hello" from the associative array.
I agree it's inconsistent, and it bothers me, too.
Nov 05 2003
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
How about this syntax:

	array_var.remove(index_val);

???
Nov 05 2003
parent "Walter" <walter digitalmars.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:bobuqu$1017$1 digitaldaemon.com...
 How about this syntax:

 array_var.remove(index_val);
probably something like that will have to be done.
Nov 05 2003