Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
February 10, 2006 Concatenating with a string array | ||||
---|---|---|---|---|
| ||||
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 Re: Concatenating with a string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | 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 Re: Concatenating with a string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yves Jacoby | "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 Re: Concatenating with a string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Attachments: | 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 Re: Concatenating with a string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | "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 Re: Concatenating with a string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: Concatenating with a string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright |
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 Re: Concatenating with a string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright Attachments: | 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 |
Copyright © 1999-2021 by the D Language Foundation