++Phobos

rikki cattermole rikki at cattermole.co.nz
Thu Oct 17 12:05:32 UTC 2019


On 18/10/2019 12:48 AM, Jacob Carlborg wrote:
> Can't we just allow structs to implement interfaces and use template 
> specialization? Something like this:
> 
> interface InputRange(ElementType)
> {
>      ElementType front();
>      bool empty();
>      void popFront();
> }
> 
> struct Range(E) : InputRange!E
> {
>      E front() { return E.init; }
>      bool empty() { return true; }
>      void popFront() { }
> }
> 
> void foo(T : InputRange!int)(T range) {}
> 
> void bar()
> {
>      InputRange!int a = Range!int.init; // will not compile
> }
> 
> -- 
> /Jacob Carlborg

That is a solution yes.

The reason I do not believe it is one of the better ones is because the 
struct has to inherit from the interface (and fields are not supported). 
For reference Basile came up with this very solution over a year ago.

The way we use input ranges today and with that DbI in general, is that 
the interface inherits from the implementation as part of the 
declaration of its usage. This is a very different approach to how we 
think of classes and much more inline with signatures from ML[0].

[0] https://www.cs.cmu.edu/~rwh/introsml/modules/sigstruct.htm


More information about the Digitalmars-d mailing list