why is this cast necessary?

Ellery Newcomer ellery-newcomer at utulsa.edu
Mon Jun 7 20:47:25 PDT 2010


On 06/07/2010 10:02 PM, Graham Fawcett wrote:
> 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
>

Curious. in std.array, for string types front is defined as

dchar front(A)(A a);

ick. unicode.


More information about the Digitalmars-d-learn mailing list