While expression templates are neat, I don't think I'd trade them for the current set of operator traits due to the "trust" they allow me to currently have when reading code. Not to mention that in your example,
would need to have that first argument have consistent types, probably Box<dyn Expression>
which would require wrapping in Box::new(arg) as Box<dyn Expression>
(though maybe type inference can elide the as
part). I suppose you could have this be a huge enum
, but then it would be full of Box
es due to the recursive nature of just about every operator. I feel like a macro that matches on the tokens of the operators and builds the tree that way would work better myself. You could even then have x**2
instead of x.pow(2)
. Or 2**x
instead of the (otherwise impossible) 2.pow(x)
.
See these threads:
- `ControlFlow`: The Answer to `BoolAnd`/`BoolOr`
- A different way to override || and &&
- Pre-RFC: Overload Short Curcuits
Prior discussion: