A set type implemented as an AA wrapper

mark mark at qtrac.eu
Thu Mar 12 20:34:04 UTC 2020


On Thursday, 12 March 2020 at 16:02:14 UTC, H. S. Teoh wrote:
> On Thu, Mar 12, 2020 at 08:51:24AM +0000, mark via 
> Digitalmars-d-learn wrote: [...]
>> YYY: The range() method is clearly not good D style but I 
>> don't know
>> how to support foreach (item; aaset) ...
>
> The usual idiom is to overload .opSlice, then you can do:
>
> 	foreach (item; aaset[]) { ... }
>
> IOW rename .range to .opSlice.
>
>
>> ZZZ: I can't figure out how to support the in operator.
>
> Note that 'x in aa' returns a pointer, not a bool.  You could 
> try:
>
> 	bool opBinaryRight(string op : "in")(T lhs) {
> 		return (lhs in set) !is null;
> 	}

I renamed .range to .opSlice (and deleted the alias range this) 
and  foreach(item; aaset) works fine.

As for the != null, that was a mistake (due to Java) which I've 
now fixed.

I'm hoping to add union & intersection soon too.

Thanks.


More information about the Digitalmars-d-learn mailing list