On Sunday, 16 December 2012 at 06:38:13 UTC, anonymous wrote:
On Saturday, 15 December 2012 at 16:32:27 UTC, r_m_r wrote:
On 12/15/2012 08:57 PM, anonymous wrote:
Note that here s1alpha and s2alpha are distinct types.

what about this: http://dpaste.dzfl.pl/95f7a74d

Consider

struct Foo {mixin (genStruct!("s1"));}
struct Bar {mixin (genStruct!("s1"));}

Foo.s1alpha and Bar.s1alpha are the same type. In my version they are not.


Yes, they should be the same type. If you are inserting an actual struct with the same name("s1") then you would expect them to be identical. (else you could just do something like mixin(genStruct!("_1", "s1")); to get a different copy of the struct.

But we are not talking about inserting a field but "combining" structs.

I think the updated answer is more pertinent:

http://dpaste.dzfl.pl/d9f001db

Here we are "inserting" a struct(well, now it is simply a template) into another(not a struct field, different things).

struct B { int x; }
struct A { Insert(B); }

should be indentical to

struct A { int x; }

r_rm_r has parameterized it so that it's really A!(B) which is better so we can combine them in many ways.

What all this is analogous to is multiple inheritance at compile time. A inherits B, so to speak. It is helpful in breaking down larges structs into smaller ones and being able to mix and match.

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.

struct A
{
  int x;
  int y;
}

struct B
{
  int x;
  int z;
}

A + B <==>

struct ApB {
  int x;   // Possible error because of overlap
  int y;
  int z;
}

A - B <==>
struct AmB {
   int y;
   int z;
}

A*B <==>
struct AtB {
   int x.A;
   int x.B;
   int y;
   int z;
}

or whatever. The usefulness is mainly that you can decompose a struct into pieces then recombine them into something possibly different. I'm not interested in the algebra aspect, just the ability to break down into two pieces, one I supply, one the user of the struct supplies. A simple A + B type of operation. The point, is, a lot of possible with such a feature. Possibly no one will use it but it could be quite powerful if it was designed appropriately.




Reply via email to