how to define my own traits

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 26 17:49:14 PDT 2017


On Sunday, 26 March 2017 at 23:25:49 UTC, XavierAP wrote:
> I've looked into Phobos to emulate it when defining my own 
> trait template, and when I see this:
>
> module std.range.primitives;
> // ...
> template isInputRange(R)
> {
>     enum bool isInputRange = is(typeof(
>     (inout int = 0)
>     {
>         R r = R.init;     // can define a range object
>         if (r.empty) {}   // can test for empty
>         r.popFront;       // can invoke popFront()
>         auto h = r.front; // can get the front of the range
>     }));
>
> I wonder, why that unused parameter (inout int = 0)?

`git blame` is your friend. It's all documented in the commit 
logs:
The original version does not have that parameter [1]. Then, to 
fix using isInputRange on an inout argument [2] a dummy parameter 
was introduced to force the delegate inside isInputRange to be 
typed as inout as well [3].
Later, the dummy parameter's name was dropped [4].

> In my project () { /* ... */ } works the same for a custom 
> trait.

Have you tried it without the dummy parameter on the example 
given in the bug report [2]?

[1] 
https://github.com/dlang/phobos/commit/49f646271bf6c843fcdd90249baa875bf43be0a1#diff-b7fc67b6fcb7d1a521918d06ffe03587R50
[2] https://issues.dlang.org/show_bug.cgi?id=7824
[3] 
https://github.com/dlang/phobos/commit/ed00f6c28c602bd5c3b197e555c44d3b53ef76ba
[4] 
https://github.com/dlang/phobos/commit/db1d909d7adb1b28ec0ba1895f56da0cc01a937b


More information about the Digitalmars-d-learn mailing list