www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why is this cast necessary?

reply Graham Fawcett <fawcett uwindsor.ca> writes:
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
Jun 07 2010
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 07 Jun 2010 23:02:48 -0400, Graham Fawcett <fawcett uwindsor.ca>  
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?
In a recent update, Andrei changed char[] and wchar[] to bi-directional ranges of dchar instead of straight arrays (at least, I think that was the change) in the eyes of the range types. I think this is where the dchar comes from. If you had a char[], and the 'max' element was a sequence of 2 code points, how do you return a single char for that result? -Steve
Jun 07 2010
parent Graham Fawcett <fawcett uwindsor.ca> writes:
Hi Steve,

On Mon, 07 Jun 2010 23:46:40 -0400, Steven Schveighoffer wrote:

 On Mon, 07 Jun 2010 23:02:48 -0400, Graham Fawcett <fawcett uwindsor.ca>
 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?
In a recent update, Andrei changed char[] and wchar[] to bi-directional ranges of dchar instead of straight arrays (at least, I think that was the change) in the eyes of the range types. I think this is where the dchar comes from. If you had a char[], and the 'max' element was a sequence of 2 code points, how do you return a single char for that result?
Thank you. This makes sense. Regards, Graham
Jun 08 2010
prev sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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.
Jun 07 2010