Closure/Stack-to-Heap Semantics

bearophile bearophileHUGS at lycos.com
Thu Sep 25 06:04:26 PDT 2008


downs:
>   foreach (i; Range[0..100].endIncl) {

That's a bit ugly. Ruby uses ... to denote an interval closed on the right and .. to denote an interval open on the right. But it's easy to miss the difference when you read code, so that too is bad design.

Python always uses intervals open on the right, avoiding problems. D slices and D2 foreach interval syntax too use the same semantics.

In my d libs I use something more readable:

foreach (i; xrange(100+1)) {

If you really really want to iterate on an interval closed on the right you can use an iterable with a different name, this helps the person that reads your code spot the difference quickly, for example:

foreach (i; xinterval(100)) {

Bye,
bearophile



More information about the Digitalmars-d mailing list