Thread overview
C++ concepts
Feb 16, 2010
Justin Johansson
Feb 16, 2010
Walter Bright
Feb 16, 2010
Justin Johansson
Feb 16, 2010
bearophile
Feb 16, 2010
Justin Johansson
Feb 16, 2010
retard
Feb 16, 2010
bearophile
Feb 16, 2010
Walter Bright
February 16, 2010
This evening I was reading an article comparing Haskell and C++ and it
gave some example C++ code using a keyword I had never heard of, namely,
"concept".  I'm thinking to myself, "this ain't C++" so
to be sure to be sure to be sure
I checked out Wikipedia and soon discovered that the C++ "concept"
keyword was to be something new in C++0x only to be pulled out just
about as fast as it was put in.

Here's the Wikipedia link:

http://en.wikipedia.org/wiki/Concepts_(C%2B%2B)

and a snippet from that link:

++ template classes and functions necessarily impose restrictions on the types that they take. For instance, the standard library containers require that the contained types be assignable. Unlike the dynamic polymorphism that class inheritance hierarchies exhibit, where a function that accepts an object of type Foo& can be passed any subtype of Foo, any class can be supplied as a template parameter so long as it supports all of the operations that users of actual instantiations upon that type use. In the case of the function, the requirement an argument must meet is clear (being a subtype of Foo), but in the case of a template the interface an object must meet is implicit in the implementation of that template. Concepts provide a mechanism for codifying the interface that a template parameter must meet. (See also: type class).

Anyway, fwiw, and being a sharing and caring kind of person :-),
I was wondering if any of the concepts regarding C++0x's "concept"
keyword would be of interest to D people, especially those of you
who have an interest in metaprogramming. Hey, maybe it's something
already coming to a D2 screen near you, I don't know.

Cheers
Justin Johansson
February 16, 2010
Justin Johansson wrote:
> Anyway, fwiw, and being a sharing and caring kind of person :-),
> I was wondering if any of the concepts regarding C++0x's "concept"
> keyword would be of interest to D people, especially those of you
> who have an interest in metaprogramming. Hey, maybe it's something
> already coming to a D2 screen near you, I don't know.

D already has them, they're called "constraints."


http://www.digitalmars.com/d/2.0/cpp0x.html#concepts
February 16, 2010
Walter Bright wrote:
> Justin Johansson wrote:
>> Anyway, fwiw, and being a sharing and caring kind of person :-),
>> I was wondering if any of the concepts regarding C++0x's "concept"
>> keyword would be of interest to D people, especially those of you
>> who have an interest in metaprogramming. Hey, maybe it's something
>> already coming to a D2 screen near you, I don't know.
> 
> D already has them, they're called "constraints."
> 
> 
> http://www.digitalmars.com/d/2.0/cpp0x.html#concepts

Well, of course I knew if it was a good idea D would have it already!
February 16, 2010
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
February 16, 2010
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
February 16, 2010
Tue, 16 Feb 2010 06:28:19 -0500, bearophile wrote:

> 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;
> }

BTW, how does D handle functional dependencies between types?

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

Type class is a higher level abstraction. Some consider it to be one of the best features in languages like Haskell. The similar feature in C++0x was concept maps or something like that.

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

But even in the abandoned c++0x proposal, checking axioms was optional IIRC.
February 16, 2010
retard:
> BTW, how does D handle functional dependencies between types?

You have to ask me that again in a simpler way, with one or more examples, etc. :-) My guess is that D doesn't handle that much.


> Type class is a higher level abstraction. Some consider it to be one of the best features in languages like Haskell. The similar feature in C++0x was concept maps or something like that.

The problem is they also add some complexity to C++ that's already too much complex.
In D you don't have explicit concept maps, but you can create something simpler that has similar purposes.


> But even in the abandoned c++0x proposal, checking axioms was optional IIRC.

Checking axioms is hard in a language like C++, you need a language like Coq.
But hereI was not talking about checking axioms, I was talking about using them (assumed as true by the compiler) for optimization purposes.

Bye,
bearophile
February 16, 2010
retard wrote:
> But even in the abandoned c++0x proposal, checking axioms was optional IIRC.

As far as I know, nobody had plans to implement axioms or even knew how to.