digitalmars.D.learn - Type of complex expression
- Lubos Pintes (16/16) Feb 27 2013 Hi,
- Jonathan M Davis (14/30) Feb 27 2013 The return type of map is most definitely _not_ an array. It's a range. ...
- bearophile (8/13) Feb 27 2013 better (untested):
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? Thank
Feb 27 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
Feb 27 2013
Lubos Pintes:auto a=" 1 2 3 4 5 " .split(" ") .filter!"!a.empty" .map!"to!int(a)"; writeln(a);better (untested): auto a = " 1 2 3 4 5 " .split .map!(to!int) .writeln; Bye, bearophile
Feb 27 2013