Casting the Result of splitter() into a string array

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 17 04:58:46 PDT 2012


On Sunday, June 17, 2012 13:39:19 GreatEmerald wrote:
> A quick and simple question, but I couldn't find the answer to it
> by myself: how do I cast the return type of std.array.splitter()
> into a string[]?
> 
> According to the compiler, splitter() has a return type Result.
> How useful... I assume it's a disguised tuple? If I try to simply
> assign the result to a string array, like this:
> 
>    string[] Bits = splitter(" a b");
> 
> I get an error "Error: cannot implicitly convert expression
> (splitter(" a b")) of type Result to string[]". Then if I make an
> explicit cast, I get "Error: cannot cast from Result to string[]".
> 
> So there must be something obvious that I'm missing... I also had
> the same problem when trying to cast the result of
> std.algorithm.filter!() to string[].

They're ranges. This is probably the best explanation online at the moment:

http://ddili.org/ders/d.en/ranges.html

If you want to convert a range to an array, use std.array.array, though in the 
case of a string, to!string(range) will probably work. However, it makes no 
sense to convert the result of splitter to a string. What's the point of 
splitting them just to join them?

splitter(" a b ") is going to return a range of strings. If you were to do 
array(splitter("a b ")), you'd get ["", "a", "b"].

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list