Template constraints in D
Walter Bright
newshound1 at digitalmars.com
Sat Jun 21 12:38:32 PDT 2008
Bill Baxter wrote:
> A more natural way to declare required functions would be nice.
>
> First of all, about the isAddable example, can't you just use T.init
> instead of writing an anonymous delegate? Like
> __traits(compiles, T.init + T.init)
I think you're right.
> Anyway, for a less trivial example I think that becomes pretty
> cumbersome. E.g.:
>
> concept Stack<typename X> {
> typename value_type;
> void push(X&, const value_type&);
> void pop(X&);
> value_type top(const X&);
> bool empty(const X&);
> };
>
> is going to translate to something like this?:
>
> template Stack(T)
> {
> const Stack =
> is(T.value_type) &&
> __traits(compiles, (T t, T.value_type v) { push(t, v); }) &&
> __traits(compiles, (T t) { pop(t); }) &&
> __traits(compiles, (T t) { top(t); }) &&
> is(typeof(top(T.init))==T.value_type) &&
> __traits(compiles, (T t) { empty(t) }) &&
> is(typeof(empty(T.init))==bool);
> }
>
> I hope you can work out a way to give this a more natural syntax, like
> Concepts will have in C++0x.
You're the first I've heard say that Concepts have a natural syntax! But
let me try:
template Stack(T)
{
const Stack =
__traits(compiles,
(T t)
{ T.value_type v = top(t);
push(t, v);
pop(t);
if (empty(t)){}
});
}
The idea is that the desired operations are expressed as operations,
rather than as function signatures.
> An unrelated comment is that I don't think it is relevant or respectful
> to compare the lengths of the specs. The spec for Concepts that you
> link to includes many many more in depth examples than the D spec does.
> It's an apples vs oranges comparison. So 5 pages vs 49 pages really
> doesn't have much meaning at all.
I wished to make the point that D constraints build on what one already
knows - how to write a D expression. Concepts, on the other hand, have a
major new syntax and semantic thing that must be learned. Perhaps I
expressed the point badly, do you have a suggestion?
More information about the Digitalmars-d
mailing list