December 29, 2017
On Friday, 29 December 2017 at 13:08:38 UTC, rikki cattermole wrote:
> On 29/12/2017 12:59 PM, rjframe wrote:
>> On Fri, 29 Dec 2017 12:39:25 +0000, Nicholas Wilson wrote:
>> 
>>> On Friday, 29 December 2017 at 12:03:59 UTC, Mike Franklin wrote:
>>>
>>> The problem is that interfaces are a runtime thing (e.g. you can cast a
>>> class to an interface)
>>> structs implement compile time interfaces via template duck typing
>>> (usually enforced via an if()).
>>> you could probably write a wrapper that introspected an interface and
>>> enforced that all members were implemented.
>> 
>> I've actually thought about doing this to get rid of a bunch of if
>> qualifiers in my function declarations. `static interface {}` compiles but
>> doesn't [currently] seem to mean anything to the compiler, but could be a
>> hint to the programmer that nothing will directly implement it; it's a
>> compile-time interface. This would provide a more generic way of doing
>> stuff like `isInputRange`, etc.
>
> Or we could get signatures, which are even better still!

I was about to answer that interfaces could be used to define duck types conformity models but this would be a poor and useless addition, indeed, compared to signatures.
December 29, 2017
On Friday, 29 December 2017 at 12:03:59 UTC, Mike Franklin wrote:
> In C#, structs can inherit from and implement interfaces.
> 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

I think it's deliberate, structs are just plain dumb value types.

If I remember correctly I think Remedy's Binderoo C++ bindings implemented C++ inheritance on top of structs. You might want to look at that.

Or you could do C-style "inheritance" and slap some D magic on top of that.

Some pseudo code:
struct Base
{
  enum SubType subtype;

  int someBaseField;
}

struct Child1
{
  Base base; // Must be first
  int foo;
}

struct Child2
{
  Base base;
  float bar;
}


Base b;
Child1 c1;
Child2 c2;

base_doSomething(Base* b);
child1_doSomething(Child1* c1);
child2_doSomething(Child2* c2);


base_doSomething(cast(Base*)&c1);

switch(base.subtype)
{
  case Child1:
    child1_doSomething(cast(Child1*)&b); break;
  case Child2:
    child2_doSomething(cast(Child2*)&b); break;
}

// add some alias this and other d things to smooth things out.
December 29, 2017
On Friday, 29 December 2017 at 13:08:38 UTC, rikki cattermole wrote:
> On 29/12/2017 12:59 PM, rjframe wrote:
>> On Fri, 29 Dec 2017 12:39:25 +0000, Nicholas Wilson wrote:
>> 
>>> [...]
>> 
>> I've actually thought about doing this to get rid of a bunch of if
>> qualifiers in my function declarations. `static interface {}` compiles but
>> doesn't [currently] seem to mean anything to the compiler, but could be a
>> hint to the programmer that nothing will directly implement it; it's a
>> compile-time interface. This would provide a more generic way of doing
>> stuff like `isInputRange`, etc.
>
> Or we could get signatures, which are even better still!

+6666 for SML style signatures!
December 30, 2017
On 12/29/17 7:03 AM, Mike Franklin wrote:

> 
> Is that simply because it hasn't been implemented or suggested yet for D, or was there a deliberate design decision?
It was deliberate, but nothing says it can't actually be done. All an interface call is, is a thunk to grab the actual object, and then a call to the appropriate function from a static vtable. It's pretty doable to make a fake interface. In fact, I'm pretty sure someone did just this, I have no idea how far back in the forums to search, but you can probably find it.

Now, it would be nicer if the language itself supported it. And I don't see any reason why it couldn't be supported. The only issue I think could be an ABI difference between member calls of structs and classes, but I think they are the same.

-Steve
January 02, 2018
On Friday, 29 December 2017 at 12:59:21 UTC, rjframe wrote:
> On Fri, 29 Dec 2017 12:39:25 +0000, Nicholas Wilson wrote:
>
>> On Friday, 29 December 2017 at 12:03:59 UTC, Mike Franklin wrote:
>>
>> The problem is that interfaces are a runtime thing (e.g. you can cast a
>> class to an interface)
>> structs implement compile time interfaces via template duck typing
>> (usually enforced via an if()).
>> you could probably write a wrapper that introspected an interface and
>> enforced that all members were implemented.
>
> I've actually thought about doing this to get rid of a bunch of if qualifiers in my function declarations. `static interface {}` compiles but doesn't [currently] seem to mean anything to the compiler, but could be a hint to the programmer that nothing will directly implement it; it's a compile-time interface. This would provide a more generic way of doing stuff like `isInputRange`, etc.

Atila does something like this

https://code.dlang.org/packages/concepts


January 02, 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
January 02, 2018
On Tue, 02 Jan 2018 00:54:13 +0000, Laeeth Isharc wrote:

> On Friday, 29 December 2017 at 12:59:21 UTC, rjframe wrote:
>> On Fri, 29 Dec 2017 12:39:25 +0000, Nicholas Wilson wrote:
>>
>> I've actually thought about doing this to get rid of a bunch of if qualifiers in my function declarations. `static interface {}` compiles but doesn't [currently] seem to mean anything to the compiler, but could be a hint to the programmer that nothing will directly implement it; it's a compile-time interface. This would provide a more generic way of doing stuff like `isInputRange`, etc.
> 
> Atila does something like this
> 
> https://code.dlang.org/packages/concepts

Thanks; I actually started skimming through his repositories a couple of days ago, but didn't see this.

--Ryan
January 02, 2018
On Saturday, 30 December 2017 at 16:23:05 UTC, Steven Schveighoffer wrote:
> On 12/29/17 7:03 AM, Mike Franklin wrote:
>
>> 
>> Is that simply because it hasn't been implemented or suggested yet for D, or was there a deliberate design decision?
> It was deliberate, but nothing says it can't actually be done. All an interface call is, is a thunk to grab the actual object, and then a call to the appropriate function from a static vtable. It's pretty doable to make a fake interface. In fact, I'm pretty sure someone did just this, I have no idea how far back in the forums to search, but you can probably find it.
>
> Now, it would be nicer if the language itself supported it. And I don't see any reason why it couldn't be supported. The only issue I think could be an ABI difference between member calls of structs and classes, but I think they are the same.
>
> -Steve

Don't forget to implement boxing/unboxing and then write a long blog post on all the dangers and negative effects of using interfaces with structs.

January 02, 2018
On Tuesday, 2 January 2018 at 00:54:13 UTC, Laeeth Isharc wrote:
> On Friday, 29 December 2017 at 12:59:21 UTC, rjframe wrote:
>> On Fri, 29 Dec 2017 12:39:25 +0000, Nicholas Wilson wrote:
>>
>>> [...]
>>
>> I've actually thought about doing this to get rid of a bunch of if qualifiers in my function declarations. `static interface {}` compiles but doesn't [currently] seem to mean anything to the compiler, but could be a hint to the programmer that nothing will directly implement it; it's a compile-time interface. This would provide a more generic way of doing stuff like `isInputRange`, etc.
>
> Atila does something like this
>
> https://code.dlang.org/packages/concepts

Glad you brought this up, looks quite useful.
1 2
Next ›   Last »