Type of complex expression

Jonathan M Davis jmdavisProg at gmx.com
Wed Feb 27 02:11:27 PST 2013


On Wednesday, February 27, 2013 11:02:59 Lubos Pintes wrote:
> Hi,
> Some time ago I asked how to efficiently parse a space delimited list of
> ints to array. Ireceived a good answer, but recently I discovered this:
> auto a="  1 2  3   4 5   "
>    .split(" ")
>    .filter!"!a.empty"
>    .map!"to!int(a)";
> writeln(a);
> //writes [1, 2, 3, 4, 5] as expected.
> 
> But:
> writeln(typeid(a).stringof);
> writes something interestingly crazy.
> I expected something like "int[]". So of which type is that expression,
> and is this a good way to solve numbers parsing if I suppose that input
> is correct?

The return type of map is most definitely _not_ an array. It's a range. writeln 
understands ranges and will print them out in the same way that it will print 
out arrays, which is why the output looks the same as if it were an array, but 
almost no range-based functions return arrays, because doing so would be 
inefficient (and in the case of infinite ranges, outright impossible). The exact 
type of a range depends on what returns it, and they're always templated 
types, so they also vary based of the function arguments. It's not really 
intended that you know or care what the range types are other than the fact 
that they're ranges and therefore have the API that ranges have. If you 
actually need to convert a range to an array, then use std.array.array, but 
range-based functions won't generally do that on their own, and most of the 
time, there's no reason to convert ranges to arrays.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list