Protocols vs Concepts?

Shammah Chancellor anonymous at coward.com
Wed Nov 20 15:44:53 PST 2013


On 2013-11-20 23:35:00 +0000, Meta said:

> On Wednesday, 20 November 2013 at 23:27:38 UTC, Shammah Chancellor wrote:
>> I just watched this talk from the goto conference by Walter Bright 
>> (https://www.youtube.com/watch?v=cQkBOCo8UrE) and he makes reference to 
>> "concepts."  However,  they look almost identical to Protocols in 
>> Smalltalk -- something which I would very much like to see in D.
>> 
>> It basically works much like an interface, but instead of declaring it, 
>> you check the type to see if it defines the appropriate methods.
>> 
>> In fact -- I believe this could be implemented today with __traits and 
>> static foreach to simply check to see if a type conforms to a protocol 
>> at compile time.
>> 
>> implementsProtocol!( R, InputRange!(int) );
>> 
>> where InputRange!() could just be a templated interface.
>> 
>> Having this in phobos would be really helpful!   If there is interest 
>> in this, I'll write a template up and submit it to phobos.
>> 
>> -Shammah
> 
> Something that's almost exactly what you're looking for:
> 
> http://dlang.org/phobos/std_typecons.html#.wrap
> http://dlang.org/phobos/std_typecons.html#.unwrap
> 
> Also, note that this is pretty much how D templates work. The 
> difference between concepts and protocols is that concepts work at 
> compile time, while protocols work at runtime. Interfaces, a la Java 
> are a hybrid. That's really just an implementation detail, though. None 
> of these terms are all that well defined across all or even most 
> programming languages.


Wrap does something similar, but more (since I don't want a wrapper).  
I just want to check it at compile time.   Something like this should 
be standard instead of isInputRange!() and the plethora of "concept 
checkers" in various libraries.   It seems like we're developing a web 
of these concept-checkers and it's hard to see what I should implement 
to be an "InputRange" from the documentation.   If there was a standard 
interface for it, and a library-defined checker, it would be more 
consistent across the codebase and easier to see what specifically 
needs to be implemented.



More information about the Digitalmars-d mailing list