the Result Type

Ali Çehreli acehreli at yahoo.com
Sun Dec 8 00:45:03 PST 2013


On 12/08/2013 12:24 AM, seany wrote:

 > std.algorithm.splitter seems to return all its return values as a type
 > "Result", without quotes, and i dont not seem to be able to cast it to
 > string[] or int[]  with cast(string[]) ( or even cast (string) - i tried
 > that too).
 >
 > I tried to use a function
 >
 > void function(T, R)(T arr, out R output)
 > {
 >
 > foreach(elem; arr)
 > {
 >      output ~= elemM
 > }
 > }
 >
 > i have an occasion where R is int[], and one where it is string[]; and
 > but it says, that int can not be appended to string[], and string can
 > not be appended to int[], and compilation fails.
 >
 > Nonetheless removing either of the occasions, solves it.
 >
 > how to deal with  this Result type?

Many other algorithms return Result, which are independent from each 
other. :) (Lookup "Voldemort types".)

Such types are just lazy ranges. When you eagerly need an actual array 
of the elements, call std.array.array on Result:

import std.array;
import std.algorithm;

void main()
{
     auto input = "hello world";
     auto splittedWords = input.splitter(' ').array;

     static assert(is (typeof(splittedWords) == string[]));
}

Ali



More information about the Digitalmars-d-learn mailing list