May 07, 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.
May 07, 2014
On Wednesday, 7 May 2014 at 14:49:17 UTC, Orvid King via Digitalmars-d wrote:
> On 5/7/14, w0rp via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
>> void foo(InputRange range);
>
> How to make it accept multiple types? Simple, we already have template
> constraints, so this would be how to do it, where T is the element
> type of the input range:
>
> void foo(T)(InputRange!T range);

I am confused. The proposed syntax would already accept many different types.
May 07, 2014
On Wed, May 07, 2014 at 08:09:34PM +0000, w0rp via Digitalmars-d wrote:
> On Wednesday, 7 May 2014 at 14:49:17 UTC, Orvid King via Digitalmars-d wrote:
> >On 5/7/14, w0rp via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> >>void foo(InputRange range);
> >
> >How to make it accept multiple types? Simple, we already have template constraints, so this would be how to do it, where T is the element type of the input range:
> >
> >void foo(T)(InputRange!T range);
> 
> I am confused. The proposed syntax would already accept many different types.

I believe the objection is that the proposed syntax can't tell the difference between:

	void foo(R)(R range1, R range2) if (isInputRange!R)

and

	void foo(R,S)(R range1, S range2) if (isInputRange!R && isInputRange!S)

I.e. in the first case, the two ranges must be the same type, whereas in the second case they can be different types as long as they are both input ranges.


T

-- 
Не дорог подарок, дорога любовь.
May 07, 2014
 I believe Concepts lite in C++ works around this by allowing a syntax like this:

void foo(InputRange{T} range1, InputRange{T2} range2)

vs.

void foo(InputRange range1, InputRange range2)

If they are the same type.


> I believe the objection is that the proposed syntax can't tell the
> difference between:
>
> 	void foo(R)(R range1, R range2) if (isInputRange!R)
>
> and
>
> 	void foo(R,S)(R range1, S range2) if (isInputRange!R && isInputRange!S)
>
> I.e. in the first case, the two ranges must be the same type, whereas in
> the second case they can be different types as long as they are both
> input ranges.
>
>
> T

1 2
Next ›   Last »