Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 06, 2014 array append result type | ||||
---|---|---|---|---|
| ||||
string a; char[] b; pragma(msg, typeof(a ~ b)); // char[] why not string? What are the rules that determine this? |
October 06, 2014 Re: array append result type | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Monday, 6 October 2014 at 11:28:16 UTC, John Colvin wrote:
> string a;
> char[] b;
> pragma(msg, typeof(a ~ b)); // char[]
>
> why not string?
>
> What are the rules that determine this?
*Ideally*, I'd have said "it returns char[], so that you can chose via purity".
However, it's not pure, so that argument doesn't hold.
That said, casting to immutable is safe provided you never actually mutate. The reverse isn't true.
To answer the question directly though, I don't know what the rules are. I'd guess they are mostly "whatever druntime implemented" though...
|
October 06, 2014 Re: array append result type | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On 10/6/14 7:28 AM, John Colvin wrote: > string a; > char[] b; > pragma(msg, typeof(a ~ b)); // char[] > > why not string? It really should be whatever you want. a ~ b is going to generate a completely unique independent copy of a and b. > What are the rules that determine this? Not sure. I would have expected at least one of a ~ b or b ~ a to be string, but both generate char[]. I filed this ER ages ago: https://issues.dlang.org/show_bug.cgi?id=1654 Not sure if anyone has it on their radar at this point. -Steve |
October 06, 2014 Re: array append result type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 6 October 2014 at 16:38:37 UTC, Steven Schveighoffer wrote:
> I filed this ER ages ago: https://issues.dlang.org/show_bug.cgi?id=1654
>
> Not sure if anyone has it on their radar at this point.
>
> -Steve
I didn't read the whole thing, but wouldn't purity be a major game changer for 1654?
|
October 06, 2014 Re: array append result type | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On 10/6/14 1:01 PM, monarch_dodra wrote:
> On Monday, 6 October 2014 at 16:38:37 UTC, Steven Schveighoffer wrote:
>> I filed this ER ages ago: https://issues.dlang.org/show_bug.cgi?id=1654
>>
>> Not sure if anyone has it on their radar at this point.
>>
>> -Steve
>
> I didn't read the whole thing, but wouldn't purity be a major game
> changer for 1654?
Of course! It was immediately what I thought of when purity = unique was introduced (that and dup/idup).
This aspect has not been mentioned on the thread, but that issue is really old.
-Steve
|
October 06, 2014 Re: array append result type | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On 10/06/2014 04:40 AM, monarch_dodra wrote: > On Monday, 6 October 2014 at 11:28:16 UTC, John Colvin wrote: >> string a; >> char[] b; >> pragma(msg, typeof(a ~ b)); // char[] >> >> why not string? >> >> What are the rules that determine this? > > *Ideally*, I'd have said "it returns char[], so that you can chose via > purity". > > However, it's not pure, so that argument doesn't hold. Array concatenation should be considered pure because it is the equivalent of the following function, which currently works: pure char[] cat(string a, const(char)[] b) { return a ~ b; } void main() { string a; char[] b; char[] c = cat(a, b); // works string s = cat(a, b); // works } http://dlang.org/function.html#pure-functions We should bring the above function and the ~ operator in line with each other. I haven't followed the discussions on GC usage in purity closely but if 'new' is allowed, GC allocations should be allowed as well. Ali |
Copyright © 1999-2021 by the D Language Foundation