Google video about consepts
Reiner Pope
reiner at none.com
Sun Apr 8 18:42:05 PDT 2007
I forgot to say that I've given some thought to overloading and
concept_maps.
You could implement concept_maps quite easily. Basically you introduce a
concept_map template, and you request the map from that at the
beginning of your template, instead of checking that your given type
matches the concept. So, your template would look like:
void someFunc(T)(ConceptMap!(MyConcept, T) t) {...}
You would define in your concepts library a very general ConceptMap
implementation:
template ConceptMap(alias Concept, T)
{
static if (Concept.AreRequirementsMet!(T)) alias T ConceptMap;
else static assert(false);
}
and then, to implement the maps in userland, you create your own
specializations:
template ConceptMap(alias Concept : MyConcept, T : MyType)
{
// Define the map
}
Unfortunately, this breaks IFTI.
Overloading:
There's the possibility of writing a forwarder function which chooses
the best overload. It could be made quite nice and automated with
templates and perhaps some AST magic. Basically, the
automatically-generated forwarder would find the concepts that your
parameter matches (how? I'm not so sure...) and then list them as
parameters in order of increasing specialisation. You would allow D's
overloading rules to take control again:
template Foo(T, alias a : LowestRequiredConcept, alias b :
NextRequiredConcept, c...) {...}
template Foo(T, alias a : LowestRequiredConcept, b...) {...}
The compiler would then choose the second overload of
NextRequiredConcept isn't specified (by your forwarder) and the first if
it is.
But this ordering only works for single template parameters.
I've thought about some kind of syntax to say, "Don't use this template
-- choose a different overload;" or "prefer this template to that" but
it seems a bit tricky for user code. It seems that if we really wanted
it, and expected it to be used, there would have to be *some*
substantial change to the language to introduce it; either make it
native to the language, or allow library code to explicitly assist with
overloading.
Cheers,
Reiner
More information about the Digitalmars-d
mailing list