Jump to page: 1 24  
Page
Thread overview
One case of array assignments
Mar 12, 2013
bearophile
Mar 12, 2013
Marco Leise
Mar 13, 2013
bearophile
Mar 13, 2013
Marco Leise
Mar 13, 2013
bearophile
Mar 13, 2013
John Colvin
Mar 13, 2013
Timon Gehr
Mar 13, 2013
John Colvin
Mar 13, 2013
H. S. Teoh
Mar 13, 2013
John Colvin
Mar 13, 2013
H. S. Teoh
Mar 13, 2013
John Colvin
Mar 13, 2013
Timon Gehr
Mar 14, 2013
Marco Leise
Mar 18, 2013
monarch_dodra
Mar 18, 2013
John Colvin
Mar 18, 2013
monarch_dodra
Mar 18, 2013
Timon Gehr
Mar 18, 2013
monarch_dodra
Mar 18, 2013
Timon Gehr
Mar 19, 2013
monarch_dodra
Mar 19, 2013
Timon Gehr
Mar 19, 2013
monarch_dodra
Mar 19, 2013
Craig Dillabaugh
Mar 13, 2013
Timon Gehr
Mar 13, 2013
John Colvin
Mar 13, 2013
Timon Gehr
Mar 13, 2013
bearophile
Mar 13, 2013
H. S. Teoh
Mar 13, 2013
bearophile
Mar 13, 2013
H. S. Teoh
Mar 13, 2013
bearophile
Mar 13, 2013
bearophile
March 12, 2013
A small question relative to this issue (see it for more info):

http://d.puremagic.com/issues/show_bug.cgi?id=4565

Today this syntax compiles:

void main() {
    int[1][3] a2;
    a2[0][] = 1;
    a2[1][] = 2;
    a2[2][] = 3;
}



This used to compile fine, but today it gives warnings (and this is good):

void main() {
    int[1][3] a3;
    a3[0] = 1;
    a3[1] = 2;
    a3[2] = 3;
}


test.d(3): Warning: explicit element-wise assignment (a3[cast(uint)0])[] = 1 is better than a3[cast(uint)0] = 1
test.d(4): Warning: explicit element-wise assignment (a3[cast(uint)1])[] = 2 is better than a3[cast(uint)1] = 2
test.d(5): Warning: explicit element-wise assignment (a3[cast(uint)2])[] = 3 is better than a3[cast(uint)2] = 3


Those warnings come from this ER, that asks the [] to be always present when you perform an array operation or slice assignment (that is a basic array op):

http://d.puremagic.com/issues/show_bug.cgi?id=7444


Currently both of the following forms are accepted:

void main() {
    int[3] a0 = [1, 2, 3];
    int[1][3] a1 = [[1], [2], [3]];
    int[1][3] a2 = [1, 2, 3]; // Second syntax.
}


I don't like the confusion of items with single-item arrays, in that second syntax. (Generally in a language muddling the semantics or syntax leads to troubles later.)


Also visible here (this compiles):

void main() {
    char[3] a0 = ['a', 'b', 'c'];
    char[3] a1 = "abc";
    char[1][3] a2 = [['a'], ['b'], ['c']];
    char[1][3] a3 = ['a', 'b', 'c']; // Second syntax.
    char[1][3] a4 = "abc"; // Second syntax.
}


In issue 4565 I am suggesting to disallow the second syntax because if we are going to deprecate the assignment of array slices without using [], then maybe the idea of allowing both of those syntaxes is not good.

What do you think? Should be disallow it, or should we close issue 4565?

Thank you,
bye,
bearophile
March 12, 2013
As long as this still works, as pointed out by Kenji:

  int[3] sa = 1;  // sa is initialized to [1, 1, 1]

I'm fine with disallowing:

  int[1][3] a2 = [1, 2, 3];

But that's probably only because you didn't say:

  int[100][3] a2 = [1, 2, 3];

:D

We should keep _some_ syntax for statically initializing an array from a single element. It's a far too common task.

March 13, 2013
Marco Leise:

> But that's probably only because you didn't say:
>
>   int[100][3] a2 = [1, 2, 3];
>
> :D

I don't understand.


> We should keep _some_ syntax for statically initializing an array
> from a single element. It's a far too common task.

I agree.

What I have suggested asks to write code like [[1], [2], [3]] instead of [1, 2, 3].

Bye,
bearophile
March 13, 2013
Am Wed, 13 Mar 2013 03:45:20 +0100
schrieb "bearophile" <bearophileHUGS@lycos.com>:

> Marco Leise:
> 
> > But that's probably only because you didn't say:
> >
> >   int[100][3] a2 = [1, 2, 3];
> >
> > :D
> 
> I don't understand.

int[1][3] a2 = [1, 2, 3];

should obviously be rewritten

int[1][3] a2 = [[1], [2], [3]];

but

int[100][3] a2 = [1, 2, 3];

would likely have summoned a controversy about array
initializers with some people finding it reasonable to
do this.

> > We should keep _some_ syntax for statically initializing an
> > array
> > from a single element. It's a far too common task.
> 
> I agree.
> 
> What I have suggested asks to write code like [[1], [2], [3]] instead of [1, 2, 3].
> 
> Bye,
> bearophile

That creates an inconsistency:

// ok, to initialize array of ten with single literal
int[10] a1 = 1;
// not ok, to initialize array of ten with single literal ?
int[10][3] = [1, 2, 3];

-- 
Marco

March 13, 2013
Marco Leise:

> but
>
> int[100][3] a2 = [1, 2, 3];
>
> would likely have summoned a controversy about array
> initializers with some people finding it reasonable to
> do this.

This code compiles:

void main() {
    int[100][3] a2 = [1, 2, 3];
}

And gives at run-time:

object.Error: lengths don't match for array copy, 300 = 3


> That creates an inconsistency:
>
> // ok, to initialize array of ten with single literal
> int[10] a1 = 1;
> // not ok, to initialize array of ten with single literal ?
> int[10][3] = [1, 2, 3];

Currently that second line of code doesn't work.

Bye,
bearophile
March 13, 2013
On Wednesday, 13 March 2013 at 20:10:14 UTC, bearophile wrote:
>> int[10][3] = [1, 2, 3];
>
> Currently that second line of code doesn't work.
>
> Bye,
> bearophile

It would be really nice if it did.
March 13, 2013
On 03/13/2013 09:23 PM, John Colvin wrote:
> On Wednesday, 13 March 2013 at 20:10:14 UTC, bearophile wrote:
>>> int[10][3] = [1, 2, 3];
>>
>> Currently that second line of code doesn't work.
>>
>> Bye,
>> bearophile
>
> It would be really nice if it did.

Then what's the meaning of

int[3][3] x = [1,2,3];

Is it

int[3][3] x = [[1,2,3],[1,2,3],[1,2,3]];

or

int[3][3] x = [[1,1,1],[2,2,2],[3,3,3]];

March 13, 2013
John Colvin:

> It would be really nice if it did.

People willing to trade their language cleanliness for little handiness deserve neither and will lose both. -- Benjamin Franklin

Bye,
bearophile
March 13, 2013
On Wed, Mar 13, 2013 at 09:54:04PM +0100, bearophile wrote:
> John Colvin:
> 
> >It would be really nice if it did.
> 
> People willing to trade their language cleanliness for little handiness deserve neither and will lose both. -- Benjamin Franklin
[...]

Wait, really? Benjamin Franklin? I thought it was a quote I snitched from Slashdot:

	He who sacrifices functionality for ease of use, loses both and
	deserves neither. -- Slashdotter


T

-- 
My program has no bugs! Only undocumented features...
March 13, 2013
On Wednesday, 13 March 2013 at 20:46:35 UTC, Timon Gehr wrote:
> On 03/13/2013 09:23 PM, John Colvin wrote:
>> On Wednesday, 13 March 2013 at 20:10:14 UTC, bearophile wrote:
>>>> int[10][3] = [1, 2, 3];
>>>
>>> Currently that second line of code doesn't work.
>>>
>>> Bye,
>>> bearophile
>>
>> It would be really nice if it did.
>
> Then what's the meaning of
>
> int[3][3] x = [1,2,3];
>
> Is it
>
> int[3][3] x = [[1,2,3],[1,2,3],[1,2,3]];
>
> or
>
> int[3][3] x = [[1,1,1],[2,2,2],[3,3,3]];

the former, clearly. It directly follows from

int[3] a = 1;

Every element of the array is initialised to the value given. x is an array of arrays and hence each "element-array" is initialised to the array on the right hand side.
« First   ‹ Prev
1 2 3 4