Why don't other programming languages have ranges?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Jul 25 12:10:35 PDT 2010


On 07/24/2010 08:36 AM, Justin Johansson wrote:
> It sounds like the D PL has invented the range idiom unlike any other PL.
>
> Since the dawn of PL's, which must be about 50 years now since Lisp
> for example, it is hard to imagine a new PL inventing a completely
> new idiom as "ranges" seem to purport. Given the many academic
> arguments for ranges as opposed to iterators, why is it that the
> D PL seems to be the major if not only sponsor of the range idiom?
>
> Is D really taking the lead here and is it likely that other PL's
> will eventually take D's lead?
>
> Cheers
> Justin Johansson

I thought of this for a couple of days. Clearly as the person using 
ranges in D consistently I'm very much at risk for accepting theories 
that make me look and feel good, so probably even if I'm trying to stay 
aware of that risk, I'm too biased for my answer to be useful.

At the same time there are a few lucid points that I feel compelled to note.

First, I agree with Walter that D's arrays have been a very strong 
influence on ranges. Pointers pervade C and iterators were a natural 
generalization of them; slices pervade D and I have been very explicitly 
preoccupied for a long time to find a formalism that legitimizes and 
formalizes slices. Though it was clear that slices do offer access to 
their underlying pointers (s.ptr and s.ptr + s.length), it was also 
clear that that's an unclean mechanism, one of last resort that should 
not be part of the formalism.

Secod, clearly ranges are derivative to STL iterators, and STL iterators 
are a very solid new idea. That also invalidates this argument:

> Since the dawn of PL's, which must be about 50 years now since Lisp
> for example, it is hard to imagine a new PL inventing a completely
> new idiom as "ranges" seem to purport.

Just wind the clock back by 16 years. We're now in 1994, and there had 
been 34 years since Lisp. However Stepanov did come with a revolutionary 
new idea.

Personally I'm not as much amazed about the time taken, as about the 
fact that many people _today_ don't figure what's good about the STL. My 
theory (which I expanded in my article On Iteration) is that STL 
requires some unusual and borderline obscure PL features, and because of 
that it could only be implemented in C++ using means that were at the 
time highly non-idiomatic. The net result was that STL's beautiful 
design was difficult to distinguish from the wall of syntax that 
surrounded it. As such, most people who weren't C++ programmers didn't 
"get" it and only some C++ programmers "got" it (even Bjarne took some 
time). In my humble opinion, the design of Java, C#, and Go is proof 
that their authors didn't get the STL. Otherwise, those languages would 
be very different.

I strongly believe Walter got the STL and generic programming in 
general. He might be fuzzy about some minor details, but he is plenty 
good at plenty other things and always had a good listening ear for the 
importance of genericity.

Third, ranges were "in the air" already at the time I formalized them. 
Boost and Adobe had notions of "range", even though all their primitives 
were to expose begin() and end(), so they were essentially lackeys of 
the STL iterator abstraction. People were talking about "range" whenever 
they discussed two iterators delimiting a portion of a container. It was 
only a matter of time until someone said, hey, let's make range a 
first-class abstraction.


Andrei


More information about the Digitalmars-d mailing list