On Thu, Nov 08, 2012 at 11:51:29PM -0500, Nick Sabalausky wrote:
> On Thu, 8 Nov 2012 20:17:24 -0800
> "H. S. Teoh" <hst...@quickfur.ath.cx> wrote:
> > 
> > Actually, I just thought of a solution to the whole duck-typing range
> > thing:
> > 
> >     struct MyRange {
> >             // Self-documenting: this struct is intended to be a
> >             // range.
> >             static assert(isInputRange!MyRange,
> >                     "Dude, your struct isn't a range!"); //
> > asserts
> > 
> > 
> On Fri, 09 Nov 2012 05:18:59 +0100
> "Adam D. Ruppe" <destructiona...@gmail.com> wrote:
> > 
> > Just a note, of course it still wouldn't *force*, but maybe it'd 
> > be a good habit to start writing this:
> > 
> > struct myrange {...}
> > static assert(isInputRange!myrange);
> > 
> 
> 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.


T

-- 
Long, long ago, the ancient Chinese invented a device that lets them see
through walls. It was called the "window".

Reply via email to