On Tuesday, 16 June 2020 at 12:30:24 UTC, jmh530 wrote:
On Tuesday, 16 June 2020 at 11:31:14 UTC, Atila Neves wrote:
On Tuesday, 16 June 2020 at 11:24:05 UTC, jmh530 wrote:
On Tuesday, 16 June 2020 at 09:15:10 UTC, Atila Neves wrote:
[snip]
In the more longer-term, is the goal of the project to implement a Typescript / Go interfaces like structural type system in user space?

Yes. Other than allowing multiple interfaces, I think it's already implemented.

I'm not familiar with what Typescript does, but doesn't Go allow interfaces to be implemented by free-standing functions?

So does tardy.

Sorry, I had not realized that. I took Go's interface example and converted it to D. Would this work with tardy?

<snip>

With a few changes, yes (added missing semicolons, changed IGeometry to Geometry in `measure`, passed the current module so tardy can find the UFCS functions, added `@safe pure` to the UFCS functions:


import tardy;

interface IGeometry
{
    double area() @safe pure const;
    double perim() @safe pure const;
}
alias Geometry = Polymorphic!IGeometry;

struct Rect {
    double width, height;
}

struct Circle {
    double radius;
}

double area(Rect r) @safe pure {
    return r.width * r.height;
}

double perim(Rect r) @safe pure {
    return  2 * r.width + 2 * r.height;
}

double area(Circle c) @safe pure {
    import std.math: PI;
    return PI * c.radius * c.radius;
}

double perim(Circle c) @safe pure {
    import std.math: PI;
    return 2 * PI * c.radius;
}

void measure(Geometry g) {
    import std.stdio: writeln;
    writeln(g);
    writeln(g.area);
    writeln(g.perim);
}

void main() {
    auto r = Rect(3.0, 4.0);
    auto c = Circle(5.0);

    Geometry.create!__MODULE__(r).measure;
    Geometry.create!__MODULE__(c).measure;
}




Reply via email to