bearophile wrote:
Justin Johansson:
I was wondering if any of the concepts regarding C++0x's "concept"
keyword would be of interest to D people,
Concepts look nice on paper, but they add a good amount of complexity to a
language that's already dying for complexity overload. So in the end Concepts
will not be present in the first versions of C++x0.
D2 answer to Concepts are the Template Constraints, that are way simpler, they
don't require new keywords, and you need just seconds to learn what they are.
Template Constraints don't allow you to do everything you can do with Concepts,
but I like them, because they give you back more than the small amount of
complexity they add to the language.
One of the main purposes of Concepts is shared by Template Constraints, if you
instantiate the following Add templated function on a type that doesn't support
the plus operator, you get a compilation error at the calling point instead
inside the template.
T Add(T)(T x, T y) if (IsAddable!T) {
return x + y;
}
In practice the recent stacked error messages added by Don to D2 help find the
calling point of a template that lacks a Constraints too :-) (And G++ has a
similar feature that improves error messages).
"Groups" of Concepts are a bit less handy to manage in D compared to that C++x0
proposal, but I think I will not miss this too much.
In the proposal for Concepts there are Axioms too, and they are not present in D2. One of
the nice thing about Axioms is that they in theory will allow the user to give more
semantics to the compiler, I think this is something that will be present in future
compilers. But in practice I think all such flexibility is not that important. A language
with macros is much more flexible than Java, but in practice you can write a large number
of programs in Java. So I think it can be enough to take just the most common usages of
Axioms and to "hard-code" them in D as few D2 attributes. This can mean to add
to D just a @associative and @symmetric annotations, that can be applied to
functions/methods to denote some of their qualities, that later can be used by the
optimizer stages of the compiler.
Bye,
bearophile
Thanks for your valuable feedback bearophile.
btw. Apparently Bjarne Stroustrup worked on "concepts" for several
years and he himself, whilst being disappointed at their removal
from C++0x, also voted for their removal (from C++0x) in the end.
The C++0x "Remove Concepts" Decision
Bjarne Stroustrup
http://www.drdobbs.com/cpp/218600111
Cheers
Justin