Custom binary operators
H. S. Teoh
hsteoh at quickfur.ath.cx
Sat Dec 28 12:05:18 PST 2013
On Sat, Dec 28, 2013 at 07:35:00PM +0100, Joseph Rushton Wakeling wrote:
> On 28/12/13 18:50, John Colvin wrote:
> >how about:
> >
> >a.elementWise * b
> >
> >where a.elementWise returns a wrapper struct around a that implements
> >elementwise arithmetic as opposed to the normal arithmetic.
> >
> >The operators would return instances of Matrix, not elementWise, to
> >avoid accidentally spilling a Matrix.ElementWise struct in to ensuing
> >code unintentionally. With a shorter alias it's rather neat.
> >
> >auto e = ((a .EW* b) .EW/ c ) * d;
>
> That's a rather cool idea -- thanks for that! :-)
>
> That said, I think it'd be difficult to really justify it as a
> pattern for general use. If you're doing mathematical work à la
> MATLAB, you want a proper operator, not a trick which lets you sort
> of write something that looks a little like one. I'm a little
> disappointed there's no ready way to do this :-(
The other way is to use a compile-time DSL, which lets you implement
whatever operators you want in whatever syntax you fancy.
T
--
Today's society is one of specialization: as you grow, you learn more and more about less and less. Eventually, you know everything about nothing.
More information about the Digitalmars-d-learn
mailing list