A Briefer Syntax for Using Concepts

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Wed May 7 09:09:04 PDT 2014


On Wednesday, 7 May 2014 at 16:04:39 UTC, Nordlöw wrote:
>> void foo(T)(InputRange!T range);
>
> Update:
>
> Ahh, it seems this syntax is currently accepted by DMD. Should 
> it be?
>
>
> Original function:
>
> import std.range: isInputRange;
>
> bool allEqual(R)(R range) @safe pure nothrow if (isInputRange!R)
> {
>     import std.algorithm: findAdjacent;
>     import std.range: empty;
>     return range.findAdjacent!("a != b").empty;
> }
> unittest { assert([11, 11].allEqual); }
> unittest { assert(![11, 12].allEqual); }
> unittest { int[] x; assert(x.allEqual); }
>
> compiles.
>
>
> New function:
>
> import std.range: InputRange;
>
> bool allEqual_(T)(InputRange!T range) @safe pure nothrow
> {
>     import std.algorithm: findAdjacent;
>     import std.range: empty;
>     return range.findAdjacent!("a != b").empty;
> }
> unittest { assert([11, 11].allEqual_); }
> unittest { assert(![11, 12].allEqual_); }
> unittest { int[] x; assert(x.allEqual_); }
>
>
> fails as
>
> algorithm_ex.d(190,27): Error: template algorithm_ex.allEqual_ 
> cannot deduce function from argument types !()(int[]), 
> candidates are:
> algorithm_ex.d(184,6):        
> algorithm_ex.allEqual_(T)(InputRange!T range)
> algorithm_ex.d(191,28): Error: template algorithm_ex.allEqual_ 
> cannot deduce function from argument types !()(int[]), 
> candidates are:
> algorithm_ex.d(184,6):        
> algorithm_ex.allEqual_(T)(InputRange!T range)
> algorithm_ex.d(192,29): Error: template algorithm_ex.allEqual_ 
> cannot deduce function from argument types !()(int[]), 
> candidates are:
> algorithm_ex.d(184,6):        
> algorithm_ex.allEqual_(T)(InputRange!T range)

std.range.InputRange is an interface template. int[] is not 
implicitly convertable to InputRange!(T[]), which is in fact an 
interface to a range with element type int[] (not int)

std.range.isInputRange and std.range.InputRange are unrelated 
except that the latter satisfies the former.


More information about the Digitalmars-d mailing list