[Issue 10404] Class!T should be the class version of type T

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jun 19 05:25:06 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=10404


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #1 from bearophile_hugs at eml.cc 2013-06-19 05:25:03 PDT ---
(In reply to comment #0)

> Given a struct S with state and methods, Class!S should be a type that:

Can you list some use cases?


> * contains exactly one member, having type S

Is this going to waste some space because of struct alignment sometimes being
higher than the alignment of its members?

Also D classes are free to reorder their fields to pack them (see Issue 8873 ),
but if you put the whole D inside the class you lose that optimization.

-----------------

Is it a good idea to also add optional interfaces to Class, as shown below?


import std.stdio, std.math;

interface Shape {}

struct CircleS { int x, y, r; }
alias Circle = Class!(CircleS, Shape);

struct RectS { int x, y, w, h; }
alias Rect = Circle!(RectS, Shape);

real area(in Shape s) {
    if (cast(Circle)s) {
        const c = cast(Circle)s;
        return c.r ^^ 2 * PI;
    } else {
        const r = cast(Rect)s;
        return r.w * r.h;
    }
}

void main() {
    new Circle(5, 10, 20).area.writeln;
}


This is a bit like an Algebraic(Circle, Rect) where Circle and Rect are by
reference...

-----------------

Similarly this is a ClassTuple, it's defined with the syntax of
std.typecon.Tuple:

alias Circle2 = ClassTuple!(int,"x", int,"y", int,"r");

But probably this is enough, no need for a ClassTuple:

alias Circle3 = Class!(Tuple!(int,"x", int,"y", int,"r"));

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list