Thread overview
Concatenating with a string array
Feb 10, 2006
Nick
Feb 10, 2006
Yves Jacoby
Feb 12, 2006
Thomas Kuehne
Feb 15, 2006
Walter Bright
Feb 15, 2006
Derek Parnell
Feb 15, 2006
Chris Sauls
Feb 24, 2006
Thomas Kuehne
February 10, 2006
This should work, shouldn't it?

void main()
{
char[][] a;
a ~= "hello";    // Works
a = a ~ "world"; // Error
}

tst.d(5): incompatible types for ((a) ~ ("world")): 'char[][]' and 'char[5]'
tst.d(5): Can only concatenate arrays, not (char[][] ~ char[5])
tst.d(5): cannot implicitly convert expression ((a) ~ "world") of type int to
char[][]

Nick


February 10, 2006
On Fri, 10 Feb 2006 12:01:56 +0000, Nick wrote:

> This should work, shouldn't it?
> 
> void main()
> {
> char[][] a;
> a ~= "hello";    // Works
> a = a ~ "world"; // Error
> }
> 
> tst.d(5): incompatible types for ((a) ~ ("world")): 'char[][]' and 'char[5]'
> tst.d(5): Can only concatenate arrays, not (char[][] ~ char[5])
> tst.d(5): cannot implicitly convert expression ((a) ~ "world") of type int to
> char[][]
> 
> Nick

Why should the first one work ? I mean:
> a ~= "hello";    // Works

Yves
February 10, 2006
"Yves Jacoby" <kloune@gmail.com> wrote in message news:pan.2006.02.10.12.12.50.539634@gmail.com...
> Why should the first one work ? I mean:
>> a ~= "hello";    // Works

They should both work, as according to the Array spec,

A static array T[dim] can be implicitly converted to one of the following:
- T*
- T[]

"hello" is of type char[5].  Thus, it is implicitly convertible to char[]. Likewise, "world" is also of char[5], and should also be implicitly converted.


February 12, 2006
Nick schrieb am 2006-02-10:
> This should work, shouldn't it?
>
> void main()
> {
> char[][] a;
> a ~= "hello";    // Works
> a = a ~ "world"; // Error
> }
>
> tst.d(5): incompatible types for ((a) ~ ("world")): 'char[][]' and 'char[5]'
> tst.d(5): Can only concatenate arrays, not (char[][] ~ char[5])
> tst.d(5): cannot implicitly convert expression ((a) ~ "world") of type int to
> char[][]
>
> Nick

Added to DStess as http://dstress.kuehne.cn/run/o/opCat_20.d

Thomas


February 15, 2006
"Nick" <Nick_member@pathlink.com> wrote in message news:dshvbk$qop$1@digitaldaemon.com...
> This should work, shouldn't it?
>
> void main()
> {
> char[][] a;
> a ~= "hello";    // Works
> a = a ~ "world"; // Error
> }
>
> tst.d(5): incompatible types for ((a) ~ ("world")): 'char[][]' and
> 'char[5]'
> tst.d(5): Can only concatenate arrays, not (char[][] ~ char[5])
> tst.d(5): cannot implicitly convert expression ((a) ~ "world") of type int
> to
> char[][]

The a~="hello" should work because ~= can do two different things:

1) append an element to an array
2) append an array of elements to an array

The a~="hello" is an example of (1).

The a=a~"hello" should not work, because ~ only does (2). I'm a little concerned that modifying it to do (1) as well will introduce unanticipated problems.


February 15, 2006
On Thu, 16 Feb 2006 04:11:13 +1100, Walter Bright <newshound@digitalmars.com> wrote:

>
> "Nick" <Nick_member@pathlink.com> wrote in message
> news:dshvbk$qop$1@digitaldaemon.com...
>> This should work, shouldn't it?
>>
>> void main()
>> {
>> char[][] a;
>> a ~= "hello";    // Works
>> a = a ~ "world"; // Error
>> }
>>
>> tst.d(5): incompatible types for ((a) ~ ("world")): 'char[][]' and
>> 'char[5]'
>> tst.d(5): Can only concatenate arrays, not (char[][] ~ char[5])
>> tst.d(5): cannot implicitly convert expression ((a) ~ "world") of type int
>> to
>> char[][]
>
> The a~="hello" should work because ~= can do two different things:
>
> 1) append an element to an array
> 2) append an array of elements to an array
>
> The a~="hello" is an example of (1).
>
> The a=a~"hello" should not work, because ~ only does (2). I'm a little
> concerned that modifying it to do (1) as well will introduce unanticipated problems.

I would like to register my concern with this position.

However, the underlying issue seems to be the attempt to concatenate fixed length arrays with variable length arrays, and that string literals are sometimes only seens as fixed length arrays.

When assigning to a variable length arrays, the compiler should recognise that fixed length arrays are proper fellows to be concatenated. In fact, concatenation always implies variable length arrays so the compiler should just do it that way.

-- 
Derek Parnell
Melbourne, Australia
February 15, 2006

Walter Bright wrote:
> "Nick" <Nick_member@pathlink.com> wrote in message news:dshvbk$qop$1@digitaldaemon.com...
> 
>>This should work, shouldn't it?
>>
>>void main()
>>{
>>char[][] a;
>>a ~= "hello";    // Works
>>a = a ~ "world"; // Error
>>}
>>
>>tst.d(5): incompatible types for ((a) ~ ("world")): 'char[][]' and 'char[5]'
>>tst.d(5): Can only concatenate arrays, not (char[][] ~ char[5])
>>tst.d(5): cannot implicitly convert expression ((a) ~ "world") of type int to
>>char[][]
> 
> 
> The a~="hello" should work because ~= can do two different things:
> 
> 1) append an element to an array
> 2) append an array of elements to an array
> 
> The a~="hello" is an example of (1).
> 
> The a=a~"hello" should not work, because ~ only does (2). I'm a little concerned that modifying it to do (1) as well will introduce unanticipated problems. 
> 

Maybe the order of (1) & (2) should be inverted for the '~' operator and left as-is for the '~=' operator?

-- Chris Nicholson-Sauls
February 24, 2006
Walter Bright schrieb am 2006-02-15:
>
> "Nick" <Nick_member@pathlink.com> wrote in message news:dshvbk$qop$1@digitaldaemon.com...
>> This should work, shouldn't it?
>>
>> void main()
>> {
>> char[][] a;
>> a ~= "hello";    // Works
>> a = a ~ "world"; // Error
>> }
>>
>> tst.d(5): incompatible types for ((a) ~ ("world")): 'char[][]' and
>> 'char[5]'
>> tst.d(5): Can only concatenate arrays, not (char[][] ~ char[5])
>> tst.d(5): cannot implicitly convert expression ((a) ~ "world") of type int
>> to
>> char[][]
>
> The a~="hello" should work because ~= can do two different things:
>
> 1) append an element to an array
> 2) append an array of elements to an array
>
> The a~="hello" is an example of (1).
>
> The a=a~"hello" should not work, because ~ only does (2). I'm a little concerned that modifying it to do (1) as well will introduce unanticipated problems.

The documentation of "array ~= x" seems a bit vague.
(http://digitalmars.com/d/arrays.html) only documents (2) not (1).

Thomas