Why not allow elementwise operations on tuples?

Sergei Nosov sergei.nosov at gmail.com
Mon Jan 16 08:30:15 UTC 2023


On Friday, 13 January 2023 at 15:27:26 UTC, H. S. Teoh wrote:
> On Fri, Jan 13, 2023 at 02:22:34PM +0000, Sergei Nosov via 
> Digitalmars-d-learn wrote:
>> Hey, everyone!
>> 
>> I was wondering if there's a strong reason behind not 
>> implementing elementwise operations on tuples?
>> 
>> Say, I've decided to store 2d points in a `Tuple!(int, int)`. 
>> It would
>> be convenient to just write `a + b` to yield another 
>> `Tuple!(int,
>> int)`.
>
> I've written a Vec type that implements precisely this, using 
> tuples behind the scenes as the implementation, and operator 
> overloading to allow nice syntax for vector arithmetic.

Yeah, that's clear that such an implementation is rather 
straightforward. Although, I'm a bit confused with your 
implementation - 1. it doesn't seem to use tuples behind the 
scenes despite your claim (it uses static array) 2. `alias impl 
this;` introduces some unexpected interactions (e.g. `~` and 
`toString` are "intercepted" by the array implementation and 
yield "wrong" results).

Anyway, my original question was primarily about reasoning - why 
there's no implementation specifically for `std.Tuple`? If it's a 
"feature, not a bug" - what's the best way to provide an 
implementation on the client side?


More information about the Digitalmars-d-learn mailing list