www.digitalmars.com         C & C++   DMDScript  

D - Overloading "delete"

reply Sam McCall <tunah.d tunah.net> writes:
The "delete" operator is used to delete key/value pairs from associative 
arrays. Could it be made possible to overload this, so I can have
delete myList[i];
in a custom list class? (maybe this should be possible for arrays too, 
inefficient though).
It seems like a special case keyword that could better be used more 
generally.
Sam
Feb 09 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Sam McCall wrote:

 The "delete" operator is used to delete key/value pairs from 
 associative arrays. Could it be made possible to overload this, so I 
 can have
 delete myList[i];
 in a custom list class? (maybe this should be possible for arrays too, 
 inefficient though).
 It seems like a special case keyword that could better be used more 
 generally.
 Sam
You could always use a proxy object. -- -Anderson: http://badmama.com.au/~anderson/
Feb 09 2004
parent reply Sam McCall <tunah.d tunah.net> writes:
J Anderson wrote:

 Sam McCall wrote:
 
 The "delete" operator is used to delete key/value pairs from 
 associative arrays. Could it be made possible to overload this, so I 
 can have
 delete myList[i];
 in a custom list class? (maybe this should be possible for arrays too, 
 inefficient though).
 It seems like a special case keyword that could better be used more 
 generally.
 Sam
You could always use a proxy object.
Or just use myList.delete(i). I was just thinking it might be nice for consistency. Or maybe not, dmd doesn't think much of my coding. I was quite taken aback when this code: Iterator!(T) it=iterator(); ... if(it is ListIterator!(T)) elicited this error message: collections.d(129): interface ListIterator has no value Still puzzling over that one :) Sam
Feb 10 2004
next sibling parent reply Carlos Santander B. <Carlos_member pathlink.com> writes:
In article <c0aac3$esq$1 digitaldaemon.com>, Sam McCall says...
Or just use myList.delete(i). I was just thinking it might be nice for 
consistency.

Or maybe not, dmd doesn't think much of my coding. I was quite taken 
aback when this code:
Iterator!(T) it=iterator();
...
if(it is ListIterator!(T))

elicited this error message:
collections.d(129): interface ListIterator has no value

Still puzzling over that one :)
Sam
I don't remember who told you that, but the 'is' operator is the same as the '==='. If you want to check if 'it' is a ListIterator, do a cast. ----------------------- Carlos Santander Bernal
Feb 10 2004
parent Sam McCall <tunah.d tunah.net> writes:
Carlos Santander B. wrote:

 I don't remember who told you that, but the 'is' operator is the same as the
 '==='. If you want to check if 'it' is a ListIterator, do a cast.
Oops... I saw someone discussing an is or isa operator (like instanceof), and then i saw "is" in the spec and jumped to conclusions :( Sam
Feb 10 2004
prev sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Sam McCall wrote:

 J Anderson wrote:

 Sam McCall wrote:

 The "delete" operator is used to delete key/value pairs from 
 associative arrays. Could it be made possible to overload this, so I 
 can have
 delete myList[i];
 in a custom list class? (maybe this should be possible for arrays 
 too, inefficient though).
 It seems like a special case keyword that could better be used more 
 generally.
 Sam
You could always use a proxy object.
Or just use myList.delete(i). I was just thinking it might be nice for consistency. Or maybe not, dmd doesn't think much of my coding. I was quite taken aback when this code: Iterator!(T) it=iterator(); .... if(it is ListIterator!(T)) elicited this error message: collections.d(129): interface ListIterator has no value Still puzzling over that one :) Sam
I think it would be a good idea to be able to be able to handle deletes of blocks of objects. Also, proxy objects are often more work then it's worth. -- -Anderson: http://badmama.com.au/~anderson/
Feb 10 2004
parent reply "C" <dont respond.com> writes:
What is a proxy object ?

C
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c0c3f0$9vn$1 digitaldaemon.com...
 Sam McCall wrote:

 J Anderson wrote:

 Sam McCall wrote:

 The "delete" operator is used to delete key/value pairs from
 associative arrays. Could it be made possible to overload this, so I
 can have
 delete myList[i];
 in a custom list class? (maybe this should be possible for arrays
 too, inefficient though).
 It seems like a special case keyword that could better be used more
 generally.
 Sam
You could always use a proxy object.
Or just use myList.delete(i). I was just thinking it might be nice for consistency. Or maybe not, dmd doesn't think much of my coding. I was quite taken aback when this code: Iterator!(T) it=iterator(); .... if(it is ListIterator!(T)) elicited this error message: collections.d(129): interface ListIterator has no value Still puzzling over that one :) Sam
I think it would be a good idea to be able to be able to handle deletes of blocks of objects. Also, proxy objects are often more work then it's worth. -- -Anderson: http://badmama.com.au/~anderson/
Feb 12 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
C wrote:

What is a proxy object ?

C
  
It's an object that is used in the place of another object (kinda like boxing). It's kinda like a object that sits in between operations. However, I assumpted because it works in C++ it would work in D. However as the below example shows it doesn't work. I don't know if this is a bug or by-design. I'm going to summit it as a bug anyways. Well in the current example (note code below doesn't work): import std.c.stdio; template ListT(T) { class Proxy { this(List thelist, int i) { list = thelist; index = i;} ~this() { list.remove(index); } //T get() { return T; } private: List list; int index; } class List { T [] List; void add(T t) { List ~= t; } void remove(int i) { printf("remove %d\n", i); //... } Proxy opIndex(int i) { Proxy proxy = new Proxy(this, i); return proxy; } } } int main ( char [] [] args ) { ListT!(int).List test = new ListT!(int).List(); test.add(10); test.add(20); test.add(30); delete test[1]; //Doesn't work (C:\Program Files\DIDE\Projects\test66\test66.d(52): 'test.opIndex(1)' is not an lvalue) return 1; } -- -Anderson: http://badmama.com.au/~anderson/
Feb 12 2004