www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D almost has concepts (lite)

reply "TommiT" <tommitissari hotmail.com> writes:
TDPL says:

"D defines a simple partial ordering relation for functions: if 
foo1 can be called with the parameter types of foo2, then foo1 ≤ 
foo2."

If you made the slightest change in the wording, you'd basically 
get the current C++ concepts-lite proposal in D:

"D defines a simple partial ordering relation for functions: if 
foo1 can be called with all the possible arguments that foo2 can 
be called with, then foo1 ≤ foo2."

At first, it almost sounds like the same statement, but now this 
would compile:

void fun(T)(T) if (T.sizeof >= 2) { }
void fun(T)(T) if (T.sizeof >= 4) { }

void main()
{
     fun(42); //calls the second one
}

I don't know if D would be better like that, but I just wanted 
point out how close the spec is to saying that.
Mar 29 2013
next sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Friday, 29 March 2013 at 07:48:39 UTC, TommiT wrote:
 TDPL says:

 "D defines a simple partial ordering relation for functions: if 
 foo1 can be called with the parameter types of foo2, then foo1 
 ≤ foo2."

 If you made the slightest change in the wording, you'd 
 basically get the current C++ concepts-lite proposal in D:

 "D defines a simple partial ordering relation for functions: if 
 foo1 can be called with all the possible arguments that foo2 
 can be called with, then foo1 ≤ foo2."

 At first, it almost sounds like the same statement, but now 
 this would compile:

 void fun(T)(T) if (T.sizeof >= 2) { }
 void fun(T)(T) if (T.sizeof >= 4) { }

 void main()
 {
     fun(42); //calls the second one
 }

 I don't know if D would be better like that, but I just wanted 
 point out how close the spec is to saying that.

That sound unimplementable, sorry.
Mar 29 2013
prev sibling next sibling parent "TommiT" <tommitissari hotmail.com> writes:
On Friday, 29 March 2013 at 07:52:13 UTC, deadalnix wrote:
 On Friday, 29 March 2013 at 07:48:39 UTC, TommiT wrote:
 TDPL says:

 "D defines a simple partial ordering relation for functions: 
 if foo1 can be called with the parameter types of foo2, then 
 foo1 ≤ foo2."

 If you made the slightest change in the wording, you'd 
 basically get the current C++ concepts-lite proposal in D:

 "D defines a simple partial ordering relation for functions: 
 if foo1 can be called with all the possible arguments that 
 foo2 can be called with, then foo1 ≤ foo2."

 At first, it almost sounds like the same statement, but now 
 this would compile:

 void fun(T)(T) if (T.sizeof >= 2) { }
 void fun(T)(T) if (T.sizeof >= 4) { }

 void main()
 {
    fun(42); //calls the second one
 }

 I don't know if D would be better like that, but I just wanted 
 point out how close the spec is to saying that.

That sound unimplementable, sorry.

I think we have different interpretations of what it means when function is callable. I think that foo is callable with int argument: void foo(T)(T x) { x("asdf"); } void main() { int n; foo(n); // the function call is made } It's just that there's a bug in foo's implementation that fails to compile, but the call to foo is yet made, i.e. foo is callable with an int argument.
Mar 29 2013
prev sibling next sibling parent "TommiT" <tommitissari hotmail.com> writes:
On Friday, 29 March 2013 at 07:52:13 UTC, deadalnix wrote:
 That sound unimplementable, sorry.

Plus, to me it seems like the same thing they're proposing to implement in C++. This is a quote from the concepts-lite proposal paper http://isocpp.org/files/papers/n3580.pdf page 17: For example, we define Totally_ordered like this: template<typename T> constexpr bool Totally_ordered() { return Weakly_ordered<T>() && Equality_comparable<T>(); } The relationship between the requirements of Totally_ordered and Equality_comparable can be pictured like this: [..] Totally_ordered subsumes Equality_comparable because its requirements include those of the latter. This relation holds for any constraint predicate that explicitly includes another. It is often the case the case that constraints overlap, with neither subsuming the other. For example, this is true of the Container and Range concepts described in the previous section. [..] The subsumes relation is used to determine which of two templates is more constrained. In particular, a template T is more specialized than another, U iff they have the same generic type and the requirements of T subsume those of U. This relation is used to different templates with the same type when computing which is more specialized. Note that a constrained template is always more specialized than an unconstrained template.
Mar 29 2013
prev sibling parent "TommiT" <tommitissari hotmail.com> writes:
On Friday, 29 March 2013 at 07:52:13 UTC, deadalnix wrote:
 That sound unimplementable, sorry.

Although, I give you that it's probably very difficult to implement. The compiler should know all kinds of math and logic: void fun(T)(T) if (1.5 ^^ T.sizeof >= 194 && T.sizeof != 42) { } void fun(T)(T) if (T.sizeof ^^ 2 >= 194) { } void main(string[] args) { byte[14] arr; fun(arr); // ambiguity error } ...if we then add a third overload: void fun(T)(T) if (1.5 ^^ T.sizeof >= 194) { } ...the call to fun(arr) would evaluate to that one.
Mar 29 2013