May 02, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3971



--- Comment #10 from Don <clugdbug@yahoo.com.au> 2010-05-01 22:08:28 PDT ---
(In reply to comment #9)
> > 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?

No. According to the spec, it's not supposed to. x[] is exactly the same as x.
The [] is only required for lvalues. So
int [4] a, b, c;
a[] =  b + c; // should work
It's almost as if there's a []= operator.
At the moment, though, a[] = b+c; fails, and you need to write a[] = b[]+c[].

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



--- Comment #11 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-05-04 09:24:59 PDT ---
Such behavior is very bug-prone: in the case of tag array it does matter whether you meant array op or array itself as value, see bug 3395 comment 2. If compiler can distinguish between x and x[] it can raise error, but judging from what you tell, compiler tries to assume.

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



--- Comment #12 from Don <clugdbug@yahoo.com.au> 2010-05-04 09:43:56 PDT ---
(In reply to comment #11)
> Such behavior is very bug-prone: in the case of tag array it does matter whether you meant array op or array itself as value, see bug 3395 comment 2. If compiler can distinguish between x and x[] it can raise error, but judging from what you tell, compiler tries to assume.

Walter has just clarified this in the newsgroup: it is intended to be mandatory to use [] inside array expressions. I have made a patch which enforces this.

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



--- Comment #13 from bearophile_hugs@eml.cc 2010-08-11 08:39:21 PDT ---
While compiling this program:

void main() {
    int[1] a1;
    int[1] a2[] = a1[];
}


compatibility with C syntax produces this error message:
test.d(3): Error: cannot implicitly convert expression (a1[]) of type int[] to
int[1u][]

See also bug 4580 as a way to solve this problem.

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


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|enhancement                 |major


--- Comment #14 from bearophile_hugs@eml.cc 2010-09-28 14:57:05 PDT ---
This is not an enhancement any more, because Walter has accepted something like
this.
Also, raising its priority a little because there is a risk of this bug
becoming permanent in D2 code.

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


Denis Derman <denis.spir@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |denis.spir@gmail.com


--- Comment #15 from Denis Derman <denis.spir@gmail.com> 2010-12-06 01:26:24 PST ---
(In reply to comment #7)
> 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.

Is it really the way D currently works? Doesn't it dup the static array instead? I thought static ones were on the stack while dynamic ones were on the heap.

Denis

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


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com


--- Comment #16 from Stewart Gordon <smjg@iname.com> 2010-12-06 03:22:53 PST ---
(In reply to comment #0)
> 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

I'm not sure I like this.  What, exactly, would be the semantic difference between the rvalue b and the rvalue b[]?  Currently, they are the same, and changing this might be confusing.

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

This is how D behaves currently.

> 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.

Making it a _syntax_ error cannot be done, because D's grammar is context-free by design.

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

???

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



--- Comment #17 from Kenji Hara <k.hara.pg@gmail.com> 2011-11-02 00:04:27 PDT ---
With latest dmd (git master):
----
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]       true    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]                 true    true    true
int[]                   false   true    true

Test code:
----
import std.stdio, std.typetuple;
void main()
{
    writeln("Rhs is an array, is it compilable?");
    writeln("a\t/ b\t\ta=b\ta[]=b\ta=b[]\ta[]=b[]");
    foreach (i, Lhs; TypeTuple!(int[3], int[]))
    foreach (j, Rhs; TypeTuple!(int[3], int[]))
    {
        writef("%s\t/ %s  ", Lhs.stringof, Rhs.stringof);
        Lhs a = [0,0,0];
        Rhs b = [1,2,3];
        writef("\t%s", is(typeof({ a   = b;   })));
        writef("\t%s", is(typeof({ a[] = b;   })));
        writef("\t%s", is(typeof({ a   = b[]; })));
        writef("\t%s", is(typeof({ a[] = b[]; })));
        writeln();
    }
    writeln("\nRhs is a element, is it compilable?");
    writeln("a\t\t\ta=N\ta[]=N\ta[0..2]=N");
    foreach (Lhs; TypeTuple!(int[3], int[]))
    {
        writef("%s\t\t", Lhs.stringof);
        Lhs a = [0,0,0];
        writef("\t%s", is(typeof({ a       = 9; })));
        writef("\t%s", is(typeof({ a[]     = 9; })));
        writef("\t%s", is(typeof({ a[0..2] = 9; })));
        writeln();
    }
}

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



--- Comment #18 from bearophile_hugs@eml.cc 2011-11-15 19:33:10 PST ---
(In reply to comment #17)
> With latest dmd (git master):

Very nice. See:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=149289

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



--- Comment #19 from bearophile_hugs@eml.cc 2011-11-15 23:56:31 PST ---
(In reply to comment #18)
> See:
> 
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=149289

This is from that post:

This also means this is currently accepted:

void main() {
    int[3] a;
    a = 1;
    assert(a == [1, 1, 1]);
}


While this is not accepted:

void main() {
    int[] b = new int[3];
    b = 1;
    assert(b == [1, 1, 1]); //Error: cannot implicitly convert expression (1)
of type int to int[]
}



I'd like D to require  a[]=1  in that first case too.

I'd like the [] to be required every time an O(n) vector operation is done,
for:
- constancy with all other vector operations among two arrays, that require [];
- and to avoid unwanted (and not easy to spot in the code) O(n) operations;
- bugs and confusion in D newbies that don't have memorized all current special
cases.

On the other hand Don says that [] is only required for lvalues.

I think this boils to a new table like this:


Rhs is an array, is it compilable?
a       / b             a=b     a[]=b   a=b[]   a[]=b[]
int[3u] / int[3u]       FALSE   true    FALSE   true
int[3u] / int[]         FALSE   true    FALSE   true
int[]   / int[3u]       FALSE   true    FALSE   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


Now if there's a [] on the left, then it's an O(n) vector operation (like a
copy), otherwise it's O(1).

That also means:

void main() {
    int[] a = new int[3];
    int[] b = new int[3];
    a = b; // OK, copies just array fat reference
}

void main() {
    int[3] a, b;
    a = b; // Not OK, hidden vector op
}


I am not sure this new table is fully correct, but it's a start. Fixes of mistakes are welcomes.

-----------------------

This is an alternative proposal.

On the other hand this vector op syntax doesn't currently compile:

void main() {
    int[3] a, b;
    a[] += b;
}


So if array assign is seen as a normal vector op, then the [] is needed on the right too:


Rhs is an array, is it compilable?
a       / b             a=b     a[]=b   a=b[]   a[]=b[]
int[3u] / int[3u]       FALSE   FALSE   FALSE   true
int[3u] / int[]         FALSE   FALSE   FALSE   true
int[]   / int[3u]       FALSE   FALSE   FALSE   true
int[]   / int[]         true    FALSE   FALSE   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


Where the two cases with dynamic arrays are syntax errors to keep more symmetry:

void main() {
    int[] a = new int[3];
    int[] b = new int[3];
    a[] = b; // error
    a = b[]; // error
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------