structs inheriting from and implementing interfaces
Meta
jared771 at gmail.com
Tue Jan 2 01:35:09 UTC 2018
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
You want wrap: https://dlang.org/phobos/std_typecons.html#wrap
More information about the Digitalmars-d-learn
mailing list