D2 Feature?

Reiner Pope some at address.com
Thu Oct 11 20:52:46 PDT 2007


Daniel Keep wrote:
> Patrick Kreft wrote:
>> I want to know  that concept like C++0x is planing for D?
>> Would improve policy-based class design ^^
>> Regards
>> Patrick Kreft
> 
> I don't think so, but if I remember correctly, someone already
> implemented C++0x style concepts as a library (sorry; can't seem to find
> the post at the moment).
> 
> 	-- Daniel

I don't know if this is what you mean, but a while ago when concepts
were discussed, I posted a proof-of-concept ( :-) )implementation:
http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=51849

I haven't looked at it recently, but from memory, it allows both sides
of concept checking (checking that your template only uses the features
specified in the concept, and checking that the template parameters
match the required concept), albeit somewhat verbosely, by effectively
writing a one-line "static unittest" which instantiates your template
with the minimum requirements, and also single static assert in the
template requiring that the parameter matches the concept.

At the time, I didn't think overloading was possible, because the
compile would simply fail if the static assert failed. However, Sean
Kelly (I think) recently posted a clever way to get around this:

void foo(T, bool TMatches : bool = MatchesConcept!(T))(T t) {...}

This is a good step further, as it allows overloads, but it doesn't
handle the case where two overloads match, yet one should be more
specialised than the other. For instance, if one template works on the
Iterator concept and one works on the RandomAccessIterator concept, it
should choose the RandomAccessIterator, but currently it just gives an
ambiguity error.



Overall, though, I think a library implementation would be very verbose
in comparison to C++ (or Haskell type-classes), the fundamental reason
being the need to write a "static unittest" for every template and a
Sean Kelly-inspired bool specialisation for every concept-checked
template parameter. When you consider that this checking should ideally 
be written for every templated function you write, it becomes excessive.

To make it nice, some form of compiler assistance is required.

    -- Reiner



More information about the Digitalmars-d mailing list