why is this cast necessary?
Graham Fawcett
fawcett at uwindsor.ca
Mon Jun 7 20:02:48 PDT 2010
Hi folks,
This program works as expected in D2:
import std.stdio;
import std.algorithm;
T largestSubelement(T)(T[][] lol) {
alias reduce!"a>b?a:b" max;
return cast(T) max(map!max(lol)); // the cast matters...
}
void main() {
auto a = [[1,2,3],[4,5,6],[8,9,7]];
assert (largestSubelement(a) == 9);
auto b = ["howdy", "pardner"];
assert (largestSubelement(b) == 'y');
auto c = [[1u, 3u, 45u, 2u], [29u, 1u]];
assert (largestSubelement(c) == 45u);
}
But if I leave out the 'cast(T)' in line 7, then this program will not
compile:
lse.d(6): Error: cannot implicitly convert expression
(reduce(map(lol))) of type dchar to immutable(char)
lse.d(14): Error: template instance
lse.largestSubelement!(immutable(char)) error
instantiating
Where did the 'dchar' came from? And why does the cast resolve the issue?
Best,
Graham
More information about the Digitalmars-d-learn
mailing list