RFC: naming for FrontTransversal and Transversal ranges

bearophile bearophileHUGS at lycos.com
Sat May 2 10:21:14 PDT 2009


About the issue of matrices as reference or value semantics.

As a program I think references are more efficient, they are used everywhere in many languages like Python/Numpy, Sage, Ruby, etc, and I don't like the idea of putting "ref" everywhere. So I like references more. Also all objects in D are reference-based, so a D programmer is already very used to the concept of reference-based data (the problems in D1 don't come from the fact that arrays are references, but from the fact that their pointer/length is passed by value, so for example if you change the length inside a function, such change can't be seen outside. So by default for dynamic arrays I'd like a full reference semantic).

On the other hand the value semantics is the most natural. Python uses reference semantics as an optimization mean, and not because references are more natural for programming newbies.
To prove this, every day in the python newsgroup people don't understand why:
if the following builds a list (array) of 10 integers:

>>> v = [0] * 10
>>> v
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> v[1] = 5
>>> v
[0, 5, 0, 0, 0, 0, 0, 0, 0, 0]

The following doesn't build a 3*4 matrix:
>>> m = [[0] * 3] * 4
>>> m[1][2] = 6
>>> m
[[0, 0, 6], [0, 0, 6], [0, 0, 6], [0, 0, 6]]

The answer comes from the value semantics of lists in python, plus immutability of natural numbers.
Such really common errors in newbies show that reference semantics isn't natural.
A future Python-like language that wants to be higher-level and less error-prone and more natural than Python will probably use value semantics everywhere (with COW, etc).

And I agree with Andrei Alexandrescu that reference semantics may produce bugs far away from their originating point.

The future of programming is more toward immutable values and functional programming, like the immutable strings of D2 (and some very intelligent people have invented clever purely functional data structures that minimize data copying).
In a purely functional language there's no point in giving the programmer a way to tell references apart from values. They can be seen as the same thing.

Bye,
bearophile



More information about the Digitalmars-d mailing list