On Sunday, 16 December 2012 at 15:34:34 UTC, Philippe Sigaud
wrote:
One could also think of it as an algebra of structs.
It would be nice to be able to do stuff like
A + B, A - B(possibly useless), A*B(possibly useless), etc...
A + B would just combine the members, A - B could remove the
members that
overlap, A*B could qualify overlapping members.
Usually, given types A and B, A+B means `A or B` (in D,
std.typecons.Algebraic!(A,B) ) and A*B means the tuple
containing A and B
(in D, std.typecons.Tuple!(A,B) ).
What you're trying to do is doable in D, but you'd have to
define it a bit
more:
if we have
struct A { int a; int b}
struct B { int b; int a} // just a and b swapped.
What is `A+B`? `A-B`?
(Thanks for the typeof(this) works great!)
Yes, A+B = A or B. Or is inclusive though.. and "overlap" and
order is immaterial.
So `A+B` is equivalent to
struct ApB { int a; int b; }
Since order ultimately matters(the layout of the struct in
memory) we could define
`B+A` =
struct BpA { int b; int a; }
`A-B` = `B-A` = (only in this case)
struct AmB { } = struct BmA { }
So A*B = std.typecons.Algebraic!(A,B) which is what I initially
said * should be. That should take care of that operation. How do
you do the other ops?