in operator generalization

Stewart Gordon smjg_1998 at yahoo.com
Wed Mar 22 04:33:19 PST 2006


Ivan Senji wrote:
<snip>
> Let's say I'm implementing a word counting program in D.
> I could do it like this:
> 
> int[char[]] words;
> Then for each word words[word]++;
> 
> But if I wanted to use normal arrays the one storing words would be:

Why would you want to make life harder by using normal arrays instead?

> char[] [] words;
> int[] count;
> 
> Now I have a word and a common thing would be to find out if it is in
> 'words'.
> 
> I would like to be able to simply write something like:
> 
> if((auto x = word in words) == -1 )

std.string.find already has these semantics.  How about having a version 
of this function for other array types?

> {
>   words.length = words.length ++;

That strikes me as undefined behaviour.  Either it's a complete no-op 
(increment words.length and then reassign the original value) or it'll 
assign words.length to itself and then increment it.

>   count.length = count.length ++;
> }

<snip>
> But what I wrote above makes sense? Doesn't it? Isn't that what everyone
> would expect in for arrays to do?

When people discover that in works on LAs as well as AAs, they will 
expect the result to be equally usable directly as a boolean value.

> Actually I don't care that much if it return a pointer or an index.
> Pointer might be a bit more useful.
> 
> y[x in y] ++; vs. (*(x in y)) ++;

Indeed.

> Although this is the way I would expect things to work there seem to be
> those thinking that there should be a way to check if there is a certain
> value in the associative array. Is this really needed?

I guess it might have a generic programming advantage in templates that 
can work on either kind of an array.  But can anyone think of a 
practical example?

> Do I really want to know which word has a certain frequency in a text?

That's not the only purpose.  It could be used to implement 
bidirectional mappings in general.

> Actually I had to do something like that once but I was able to do it by
> constructing a reverse AA (For example char[] [int]),
<snip>

Yes, that's a way to do bidirectional mappings that might be more 
efficient than value searching at the moment.  Yet another approach 
would be a single data structure with two hash tables.

What you say makes sense on the whole.  But I recall it being stated 
somewhere in the docs (I forget where) that every operator has an 
intended meaning that should be taken into account when overloading. 
Language builtins in particular have a moral duty to set a good example. 
  It's true that the current meaning of in and that being proposed have 
something in common, namely that they check for containment of the what 
you consider the "interesting" feature in each case.  But "interesting" 
is a highly subjective concept, varying both from person to person and 
from application to application, and you could argue either way on 
whether a given judgement of "interesting" is an acceptable basis of 
what the overloaded uses of one operator have in common.

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.



More information about the Digitalmars-d mailing list