The year is 2019

Mike Franklin slavo5150 at yahoo.com
Sat Jul 27 04:26:01 UTC 2019


On Saturday, 27 July 2019 at 04:15:38 UTC, XavierAP wrote:

> I think struct inheritance (with no virtual dispatch) may be 
> good for D. Has no one ever tried a DIP?

I'm formulating one, but I haven't gotten very far yet.  Others 
have also mentioned interest.  Walter is quite opposed to 
implicit casting (https://github.com/dlang/dmd/pull/10161) so I 
was thinking of a different strategy that would use keep the 
implicit casting feature of `alias this` but defer the forwarding 
to members to D's metaprogramming facilities.

> Allowing inheritance or alias this to be multiple is a separate 
> debate; I tend to think there's no sane use case. As is (but 
> related) allowing structs to implement interfaces (as in C# or 
> Go? And without violating their value semantics).

I think something like this should work:

struct IStream
{
     string read() { return ""; }
}

struct OStream
{
     void write(string s) { };
}

struct IOStream
{
     IStream i;
     OStream o;

     // Generating the following functions can be automated with 
D's awesome
     // metaprogramming facilities
     string read() { return i.read(); }
     void write(string s) { o.write(s); }

     // But how do we implicitly convert from `IOStream` to 
`IStream` or
     // `OStream` without `alias this`, `opImplicitCast` or some 
other?
     alias i this;
     alias o this; // mulitple `alias this` currently not 
implemented, so
                   // this doesn't work
}

extern string source(ref IStream i);
extern void sink(ref OStream o);

void main()
{
     IOStream io;

     source(io); // Won't work without some kind of implicit 
casting
     sink(io);   // Won't work without some kind of implicit 
casting
}

Mike


More information about the Digitalmars-d mailing list