www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Concatenating with a string array

reply Nick <Nick_member pathlink.com> writes:
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
Feb 10 2006
next sibling parent reply Yves Jacoby <kloune gmail.com> writes:
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
Feb 10 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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.
Feb 10 2006
prev sibling next sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- iD8DBQFD7vNI3w+/yD4P9tIRAhcuAJoCb5RZOeKX3ILZvTKyq5LUYDUGtACglZjb 8xfNuPKeGcoINBIWVjj10fg= =mmV/ -----END PGP SIGNATURE-----
Feb 12 2006
prev sibling parent reply "Walter Bright" <newshound digitalmars.com> writes:
"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.
Feb 15 2006
next sibling parent "Derek Parnell" <derek psych.ward> writes:
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
Feb 15 2006
prev sibling next sibling parent Chris Sauls <ibisbasenji gmail.com> writes:
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
Feb 15 2006
prev sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- iD8DBQFD/rqU3w+/yD4P9tIRAvm0AJ9ANAnttjBLDgrDeEo4PxjtdgMNYQCfcmk8 oRYsDKYliGegsqLZwx2g9RY= =tCN3 -----END PGP SIGNATURE-----
Feb 23 2006