Feedback on Átila's Vision for D
Paul Backus
snarwin at gmail.com
Tue Oct 15 16:39:47 UTC 2019
On Tuesday, 15 October 2019 at 16:17:57 UTC, Atila Neves wrote:
> Meh:
>
> //C++20 concepts
> concept isAddable = requires (T x) { x + x; };
>
> // D
> enum isAddable(T) = is(typeof((T x) => x + x));
>
>
> We lose by one character ;)
The one advantage C++ concepts have over D's template predicates
is that they're transparent to the compiler. As a result, the
compiler is able to determine a partial order on template
constraints [1], and use that order to resolve overloads (just
like D does with regular function overloads [2]). D can't do
this, so we're stuck writing awkward things like this:
auto myAlgorithm(R)(R r) if (isInputRange!R &&
!isRandomAccessRange!R) { ... }
auto myAlgorithm(R)(R r) if (isRandomAccessRange!R) { ... }
...or this:
auto myAlgorithm(R)(R r) if (isInputRange!R)
{
static if (isRandomAccessRange!R)
return myRandomAccessRangeImpl(r);
else
return myInputRangeImpl(r);
}
...in order to make our constraints both disjoint and exhaustive.
[1]
https://en.cppreference.com/w/cpp/language/constraints#Partial_ordering_of_constraints
[2] https://digitalmars.com/articles/b20.html
More information about the Digitalmars-d
mailing list