Protocols vs Concepts?

Meta jared771 at gmail.com
Wed Nov 20 15:35:00 PST 2013


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.


More information about the Digitalmars-d mailing list