On Friday, 29 December 2017 at 12:03:59 UTC, Mike Franklin wrote:
In C#, structs can inherit from and implement interfaces.

----
using System;

interface IPrint
{
    void Print();
}

struct MyStruct : IPrint
{
    public void Print()
    {
        Console.WriteLine(ToString());
    }
}

public class Program
{
    public static void Main()
    {
        MyStruct s = new MyStruct();
        s.Print();
    }
}
----
https://dotnetfiddle.net/lpXR1O

But in D it doesn't appear possible.
----
import std.stdio;

interface IPrint
{
    void print();
}

// Error: base classes are not allowed for struct, did you mean ;? struct MyStruct : IPrint // Error: base classes are not allowed for struct, did you mean ;?
{
    void print()
    {
        writeln("MyStruct");
    }
}

void main()
{
        MyStruct s;
    s.Print();
}
----
https://run.dlang.io/is/j4xwla

Is that simply because it hasn't been implemented or suggested yet for D, or was there a deliberate design decision?

Thanks for your insight,

Mike

This feature is criticized among the C# community:

https://blogs.msdn.microsoft.com/abhinaba/2005/10/05/c-structs-and-interface/

no vtable means for example that you cannot back to something after extracting the interface (with classes you can always get back to Object)

as in D, struct should really only be used for a custom type with value semantic.

Reply via email to