reduce string ranges
Nick Sabalausky
a at a.a
Mon Jun 16 17:50:24 PDT 2008
"Christian Kamm" <kamm-incasoftware at removethis.de> wrote in message
news:g27rd9$1l7d$1 at digitalmars.com...
> Brian Myers wrote:
>> The following:
>>
>> int main(char[][] args)
>> {
>> string[] tstarr = ["one", "two", "three"];
>> writefln(reduce!("a ~ b")("", tstarr));
>> }
>>
>> gives the following error message:
>>
>> c:\dmd\bin\..\src\phobos\std\algorithm.d(289): Error: functions cannot
>> return static array invariant(char[0u])
>
> This is just the old problem that string literals are static arrays.
> Reduce
> expects its return type to have the same type as the first argument, which
> is invariant(char[0]) in your case. Replace "" by ""[] and the code
> compiles.
>
1. Why does reduce need the seed to be manually specified anyway? Why not
just take the first element of the first range as the seed?
2. If it did that, that still wouldn't solve the problem from the original
post, would it? I assume you'd need to do something weird like "auto tstarr
= ["one"[], "two"[], "three"[]];"?
3. I'm still getting my head around D2's strings so this'll probably sound
stupid, but why can't "" be implicitly converted to ""[]? (and how exactly
does that solve the original problem anyway)?
BTW, ""[] means "a full start-to-end slice of the empty string literal",
right? Also, I know arrays and array slices are distict types, but what
exactly is the difference? My intuition would tell me that they're both a
pointer and a length, and an array slice is simply one that just happens to
point to a portion of another array, but lately I've been getting the
impression that there's something more to it than that?
More information about the Digitalmars-d
mailing list