On Thu, Nov 08, 2012 at 09:37:00PM -0800, H. S. Teoh wrote:
> On Thu, Nov 08, 2012 at 11:51:29PM -0500, Nick Sabalausky wrote:
[...]
> > Those are only half-solutions as they only prevent false-negatives,
> > not false-positives. Plus, there's nothing to prevent people from
> > forgetting to do it in the first place.
> 
> IOW, you want the user-defined type to declare that it's an input
> range, and not just some random struct that happens to have input
> range like functions?
> 
> What about modifying isInputRange to be something like this:
> 
>       template isInputRange(R) {
>               static if (R.implementsInputRange &&
>                       /* ... check for input range properties here */)
>               {
>                       enum isInputRange = true;
>               } else {
>                       enum isInputRange = false;
>               }
>       }
> 
> Then all input ranges will have to explicitly declare they are an
> input range thus:
> 
>       struct MyInpRange {
>               // This asserts that we're trying to be an input range
>               enum implementsInputRange = true;
> 
>               // ... define .empty, .front, .popFront here
>       }
> 
> Any prospective input range that doesn't define implementsInputRange
> will be rejected by all input range functions. (Of course, that's just
> a temporary name, you can probably think of a better one.)
> 
> You can also make it a mixin, or something like that, if you want to
> avoid the tedium of defining an enum to be true every single time.
[...]

Here's a slight refinement:

        // Note: untested code
        mixin template imAnInputRange() {
                static assert(isInputRange!(typeof(this)));
                enum implementsInputRange = true;
        }

        struct MyInpRange {
                // This should croak loudly if this struct isn't a valid
                // input range. Omitting this line makes range functions
                // reject it too (provided we modify isInputRange as
                // described above).
                mixin imAnInputRange;

                // implement range functions here
        }


T

-- 
It only takes one twig to burn down a forest.

Reply via email to