On 10/18/2015 09:00 PM, rcorre wrote:
SuperStruct is a struct that acts like a class:

---
struct Square {
   float size;
   float area() { return size * size; }
}

struct Circle {
   float r;
   float area() { return r * r * PI; }
}

alias Shape = SuperStruct!(Square, Circle);

// look! polymorphism!
Shape sqr = Square(2);
Shape cir = Circle(4);
Shape[] shapes = [ sqr, cir ];

// call functions that are shared between the source types!
assert(shapes.map!(x => x.area).sum.approxEqual(2 * 2 + 4 * 4 * PI));
---

SuperStruct is basically a Variant that exposes the common members of
its source
types. You can check it out here:

https://github.com/rcorre/superstruct

I'm not quite sure if this is a good idea (or if it already exists in some
form that I haven't seen), but it was fun to work on. There's a lot more
info on
the README if you're curious. Let me know what you think!

"A call signature for a given member is 'compatible'
* if, for an instance of any one of `SubTypes`, that member can be called with * the provided set of arguments _and_ all such calls have a common return type."


Probably you could/should return your SuperStruct instead of the/when there is no common return type.

Reply via email to