Protocols vs Concepts?

Dicebot public at dicebot.lv
Wed Nov 20 15:36:54 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.

"concept" is a term from C++ standard discussions which is a bit 
closer to D and thus has priority ;)

> 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.

It has been implemented a dozen of times at the very least, I 
have personally done it twice :D Key problem with having it as a 
library type is that it
a) does not improve error messages at all over currently used 
constraints (isInputRange!T) - it can be potentially fixed by 
improving constraint error detection

b) it is impossible to do partial semantic validation of template 
body based on concept limitation which is the original motivating 
reason behind concept designs in C++ (with no success so far)

So yeah, it is neat, but not neat enough for anyone to actually 
bother to push it into Phobos :) If you want to do it, it is 
worth noting that you don't necessarily need to restrict to 
templated interfaces, normal are also legit.


More information about the Digitalmars-d mailing list