tardy v0.0.1 - Runtime polymorphism without inheritance

jmh530 john.michael.hall at gmail.com
Tue Jun 16 12:30:24 UTC 2020


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?

```
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) {
     return r.width * r.height
}

double perim(Rect r) {
     return  2 * r.width + 2 * r.height
}

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

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

void measure(IGeometry 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);

     r.Geometry.measure;
     c.Geometry.measure;
}
```

>
>> That is a little bit more similar to open methods. This 
>> requires the type inherit from the interface and implement 
>> member functions.
>
> There is no inheritance anywhere, otherwise that'd defeat the 
> point of the library in the first place. I used interfaces 
> because they exist and intuively make sense, and support 
> classes because why not. Otherwise it could be just structs and 
> other values with candidate UFCS functions.

Sorry, that was me being hasty. I was just concerned about the 
member functions part of it, which you said above is not a 
concern.


More information about the Digitalmars-d-announce mailing list