Jump to page: 1 2 3
Thread overview
[Issue 3971] New: Syntax & semantics for array assigns
Apr 14, 2010
Sobirari Muhomori
Apr 16, 2010
Sobirari Muhomori
Apr 19, 2010
Sobirari Muhomori
Apr 20, 2010
Don
May 01, 2010
Sobirari Muhomori
May 02, 2010
Don
May 04, 2010
Sobirari Muhomori
May 04, 2010
Don
Dec 06, 2010
Denis Derman
Dec 06, 2010
Stewart Gordon
Nov 02, 2011
Kenji Hara
Nov 19, 2011
timon.gehr@gmx.ch
Nov 19, 2011
Don
Feb 03, 2012
yebblies
Feb 05, 2012
yebblies
Feb 05, 2012
timon.gehr@gmx.ch
Feb 05, 2012
yebblies
March 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971

           Summary: Syntax & semantics for array assigns
           Product: D
           Version: 2.041
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-03-15 14:55:47 PDT ---
This is written in the page about arrays: http://www.digitalmars.com/d/2.0/arrays.html

s[] = t;      // the 3 elements of t[3] are copied into s[3] s[] = t[];    // the 3 elements of t[3] are copied into s[3] s[] = 3;      // same as s[0] = 3, s[1] = 3, s[2] = 3 p[0..2] = 3;  // same as p[0] = 3, p[1] = 3


It's not good to have two different syntaxes to do the same thing, or two similar syntaxes with a different computational complexity.

So I suggest to modify the array assign syntax this way:


a[] = b[];  static  dynamic
static      OK1     OK1
dynamic     OK1     OK1

a = b[];    static  dynamic
static      Err     Err
dynamic     Err     Err

a[] = b;    static  dynamic
static      Err     Err
dynamic     Err     Err

a = b;      static  dynamic
static      Err2    Err
dynamic     Err     OK2

int i; a=i; static  dynamic
            Err     Err

int i;
a[] = i;    static  dynamic
            OK3     OK3

Key:
  Err =  Syntax error
  OK1 =  Copies all items from an array to the oter.
  OK2 =  Copies just the stuct of the dynamic array, array body not copied.
  OK3 =  Copies the value to all the items of the array.
  Err2 = Syntax error, becase there is no reference to copy, better to keep
tidy the language.


You can see that this too is disallowed:
int a, b;
a = b;

This breaks generic code, but it's not good when the same syntax can be O(1)
(because the same syntax used on dynamic arrays is a O(1)) or O(n), and the too
is different.

If you have comments please add them.

I like that matrix because it contains all cases, so it's easy to see and design the situation, even if you don't agree with its contents.

The accepted final version of that matrix can even be added back to the arrays page.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971



--- Comment #1 from bearophile_hugs@eml.cc 2010-04-12 17:41:46 PDT ---
But be careful, in this code Tri c[] is seen as Tri[] c, and it doesn't compile:

alias double[3] Tri;
void main() {
    Tri a = [1, 2, 3];
    Tri b = [10, 20, 30];
    Tri c[] = a1[] - b1[]; // ERR
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 14, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971



--- Comment #2 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-04-14 11:07:25 PDT ---
What "static", "dynamic", "a" and "b" mean? And those diagrams?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 14, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971



--- Comment #3 from bearophile_hugs@eml.cc 2010-04-14 12:09:37 PDT ---
"a" and "b" are arrays.

"dynamic" means dynamic array.

"static" means stack-allocated fixed-sized array.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 16, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971



--- Comment #4 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-04-16 13:08:07 PDT ---
a = b;      static  dynamic
static      Err2    Err
dynamic     Err     OK2
^ As I understand, this disallows assignment of a static array to the dynamic
one?

Is this related to bug 3395?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971



--- Comment #5 from bearophile_hugs@eml.cc 2010-04-18 16:27:47 PDT ---
This:
a = b;      static  dynamic
static      Err2    Err
dynamic     Err     OK2

Means:
int[5] a, b;
a = b; // Err2
int[] c = new int[5];
a = c; // Err
c = a; // Err
int[] d = new int[5];
c = d; // OK

But:
a[] = c[]; // OK
c[] = a[]; // OK

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971



--- Comment #6 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-04-19 14:11:17 PDT ---
Why c=a; is an error?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971



--- Comment #7 from bearophile_hugs@eml.cc 2010-04-19 14:42:31 PDT ---
You are right, the c=a; case can be allowed, it can just copy the ptr and length of the static array inside the struct of the dynamic array (this is how D currently works). Thank you for spotting it.

All this discussion looks academic because so far Walter seems uninterested in this enhancement request.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 20, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au


--- Comment #8 from Don <clugdbug@yahoo.com.au> 2010-04-19 17:30:28 PDT ---
(In reply to comment #7)
> All this discussion looks academic because so far Walter seems uninterested in this enhancement request.

Bearophile, please stop making absurd statements like that one. If Walter makes no comment on something, you can't conclude *anything* about his attitude to it. He's just extremely busy.
---

Almost all bugs and weird behaviour involving array operations happen because internally the compiler doesn't distinguish between  x[] and x, where x is a dynamic array. This causes a multitude of problems, especially when multidimensional arrays are involved.

I agree with you that this syntax is problematic. I don't understand why it's currently permitted:

int[3] s;
int[3] t;
s[] = t;      // the 3 elements of t[3] are copied into s[3]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 01, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971



--- Comment #9 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-05-01 08:14:28 PDT ---
> internally the compiler doesn't distinguish between  x[] and x, where x is a dynamic array.
This means, that array ops are a huge hack?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2 3