Return type of std.algorithm.map
James Fisher
jameshfisher at gmail.com
Thu Jul 7 07:00:48 PDT 2011
To date, I've been using D in much the same way I used C++, without heavy
use of templates. Now I'm trying out a more functional style using
std.algorithm. However I'm pretty much stuck at the first hurdle: map.
With type inference, this works:
import std.algorithm;
import std.stdio;
void main() {
auto start = [1,2,3,4,5];
auto squares = map!((a) { return a * a; })(start);
writeln(squares);
}
Without type inference (obviously necessary beyond trivial examples), it'd
be nice to do:
import std.algorithm;
import std.stdio;
void main() {
int[] start = [1,2,3,4,5];
int[] squares = map!((a) { return a * a; })(start);
writeln(squares);
}
but this gives "Error: cannot implicitly convert expression (map(start)) of
type Result to int[]". That opaque type "Result" is weird (not
parameterized on "int"?), but OK, let's try that:
import std.algorithm;
import std.stdio;
void main() {
int[] start = [1,2,3,4,5];
Result squares = map!((a) { return a * a; })(start);
writeln(squares);
}
gives "undefined identifier Result". Not sure why, but OK. I can't see
examples in the docs of explicit type declarations -- annoyingly they all
use "auto".
However, they do tell me that map "returns a range". Assuming all
definitions of ranges are in std.range, there's no such thing as a "Range"
interface, so it's not that. The most general interfaces I can see are
InputRange(E) and OutputRange(E). It certainly can't be an OutputRange, so
I guess it must satisfy InputRange, presumably type-parameterized with
"int". So this should work:
import std.algorithm;
import std.stdio;
import std.range;
void main() {
int[] start = [1,2,3,4,5];
InputRange!int squares = map!((a) { return a * a; })(start);
writeln(squares);
}
But the compiler complains "cannot implicitly convert expression
(map(start)) of type Result to std.range.InputRange!(int).InputRange".
That's weird, because "std.range.InputRange!(int).InputRange" doesn't even
look like a type.
I've tried all manner of combinations here, but nothing works. Seems like
there's something fundamental I'm not understanding.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110707/da3f7ada/attachment.html>
More information about the Digitalmars-d-learn
mailing list