Operators overloading

Jonathan M Davis jmdavisProg at gmx.com
Sun May 19 03:44:14 PDT 2013


On Sunday, May 19, 2013 12:36:00 bearophile wrote:
> Jonathan M Davis:
> > Another alternative would be to use a function like
> > std.algorithm.equal except
> > that it tested that one of the arguments was equal to the first
> > one rather than
> > just testing one argument against it. Something like
> > 
> > const Vertex opBinary(string op)(const ref Vertex vertex)
> > 
> >     if (isOneOf(op, "+", "-"))
> > 
> > {...}
> > 
> > Off the top of my head, I'm not aware of a function like that
> > in the standard
> > library, but I may just not being think of it, and it probably
> > wouldn't be all
> > that hard to write, at least if efficiency wasn't a major
> > concern.
> 
> It's named std.algorithm.canFind:
> 
> if (["+", "-"].canFind(op))

Ah, yes. I was thinking backwards. At first, I was thinking canFind(op, "+", 
"-"), which definitely doesn't do the right thing, and for some reason, it 
didn't occur to me to flip it. I've probably been up too long and should get to 
bed.

- Jonathan M Davis


More information about the Digitalmars-d mailing list