[Issue 3971] Syntax & semantics for array assigns
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Nov 19 12:33:23 PST 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3971
timon.gehr at gmx.ch changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |timon.gehr at gmx.ch
--- Comment #20 from timon.gehr at gmx.ch 2011-11-19 12:32:29 PST ---
Also from that thread:
First thing:
int[3] a=3; // kill it!!
Rest:
a[] is _just a shortcut_ for a[0..$]! Are you really suggesting to disallow
slicing?
You are thinking too much in terms of syntax and not enough in terms of
semantics.
They are basically two distinct things involved here:
1. static arrays and lvalue slices
2. dynamic arrays and rvalue slices
1 implies value semantics, 2 implies reference semantics, where value semantics
overrides reference semantics.
Any other distinction is more or less arbitrary. As you pointed out, the main
indicator of distinction is value vs reference semantics of the performed
assignments.
We certainly agree on this:
Rhs is an array, is it compilable?
a / b a=b a[]=b a=b[] a[]=b[]
int[3u] / int[3u] ? ? ? true
int[3u] / int[] ? ? ? true
int[] / int[3u] ? ? ? true
int[] / int[] ? ? ? true
Now, a dynamic array a is equivalent to a[], and a static array b is equivalent
to an lvalue slice b[]=.
This gives the following equivalence classes of operations:
Rhs is an array, is it compilable?
a / b a=b a[]=b a=b[] a[]=b[]
int[3u] / int[3u] 1 1 2 2
int[3u] / int[] 2 2 2 2
int[] / int[3u] 3 1 4 2
int[] / int[] 4 2 4 2
Any of the same class should behave the same.
Now, you suggest in both proposals to allow at least one of class 2 and at
least one of class 4. Filling all those out delivers:
Rhs is an array, is it compilable?
a / b a=b a[]=b a=b[] a[]=b[]
int[3u] / int[3u] (1) (1) true true
int[3u] / int[] true true true true
int[] / int[3u] (3) (1) true true
int[] / int[] true true true true
1 is "assign value to value". 3. is "assign value to reference".
The upper left corner should certainly be true, values of any mutable type
should be able to be assigned to themselves.
This leaves:
Rhs is an array, is it compilable?
a / b a=b a[]=b a=b[] a[]=b[]
int[3u] / int[3u] true true true true
int[3u] / int[] true true true true
int[] / int[3u] (3) true true true
int[] / int[] true true true true
3 is the odd thing out. Now let's think about it, what should:
int[] a;
int[3] b;
a=b;
do?
The answer is, there are two options.
1. implicitly slice b
2. copy b by value into a
One is as arbitrary as the other, so it should be disallowed in a sane design.
Which leaves:
Rhs is an array, is it compilable?
a / b a=b a[]=b a=b[] a[]=b[]
int[3u] / int[3u] true true true true
int[3u] / int[] true true true true
int[] / int[3u] FALSE true true true
int[] / int[] true true true true
Rhs is a element, is it compilable?
a a=N a[]=N a[0..2]=N
int[3u] FALSE true true
int[] false true true
And that is how it should be.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list