Short list with things to finish for D2

bearophile bearophileHUGS at lycos.com
Mon Dec 7 19:22:30 PST 2009


Stewart Gordon:

>Which doesn't accommodate anything equivalent to a[$-4 .. $-2].<

In Python:

>>> a = "abcdefgh"
>>> a[-4 : -2]
'ef'


> Or you can also use None, this can useful because you can put None
> inside a variable, etc (while in D you can't put $ inside a variable
> to represent "the end of that array"):
<snip>

>I don't understand this statement at all.<

You are right and I am sorry, let's try again.

In Python slices using None is the same as omitting the value:

a[:5] === a[None : 5]
a[5:] === a[5 : None]

Omitting a value is handy, but nothing is not a value you can pass around and store in a variable, while None allows you to do that, so it gives more flexibility. This is a little example:

def foo(start, stop):
  a = "abcdefgh"
  print a[start : stop]
  
foo(1, 2)
foo(None, 2)
foo(2, None)
foo(None, None)


Output:
b
ab
cdefgh
abcdefgh

You can't do that with omitted values, and $ too can't be moved around in a variable, so None is more flexible. (I know you can't do that in D, you need nullable integers, for example).

I hope this explanation was a little more clear.

Bye,
bearophile



More information about the Digitalmars-d mailing list