Ranges

Ary Borenszweig ary at esperanto.org.ar
Thu Jun 18 12:22:33 PDT 2009


Steve Teale wrote:
> I shall start this again from scratch. Please excuse me for being thick about it. I have tried before in more direct questions to Andrei, but in the end all I got was a snotty email telling me not to be a nuisance and RTFD. Unfortunately, the relevant documentation seems to have disappeared.
> 
> In range.d, in the context of isInputRange, it says:
> 
> "Returns $(D true) if $(D R) is an input range. An input range must
> define the primitives $(D empty), $(D popFront), and $(D front). The
> following code should compile for any input range ..."
> 
> template isInputRange(R)
> {
>     enum bool isInputRange = is(typeof(
>     {
>         R r;             // can define a range object
>         if (r.empty) {}  // can test for empty
>         r.popFront;          // can invoke next
>         auto h = r.front; // can get the front of the range
>     }()));
> }
> 
> I can not possibly be the only D enthusiast who finds this completely incomprehensible. What is a range? Is it a template interface, or is it just a trick of template syntax that supports the old assertion that "nobody really understands templates".
> 
> If ranges are to be a feature of the D language, then they should probably be supported at language level rather than by some trick that has been discovered by experimenting with how far you can push templates.
> 
> Also, it would be very useful to have some indication of what you might use them for. I occasionally had to resort to STL iterators because I wanted to use 'map'. I agree that the syntax sucked, but nobody is telling me how ranges help.
> 
> I realize that some people with an IQ of 580 will find my questions naive and misguided - not to mention impertinent, but it seems to me that one of the responsibilities of being a leader is to explain to less gifted followers what the fuck is going on. Or maybe I've got it wrong - if you're that bright (sorry Walter - not you) then perhaps it's just a big ego trip.

It's called "duck typing".

http://en.wikipedia.org/wiki/Duck_typing

"an object's current set of methods and properties determines the valid 
semantics, rather than its inheritance from a particular class or 
implementation of a specific interface"

So you don't say something is a range by looking if it implements some 
Range interface, but rather if it has some methods in it.



More information about the Digitalmars-d mailing list