digitalmars.D.learn - why is this cast necessary?
- Graham Fawcett (26/26) Jun 07 2010 Hi folks,
- Steven Schveighoffer (9/33) Jun 07 2010 In a recent update, Andrei changed char[] and wchar[] to bi-directional ...
- Graham Fawcett (5/50) Jun 08 2010 Thank you. This makes sense.
- Ellery Newcomer (4/30) Jun 07 2010 Curious. in std.array, for string types front is defined as
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
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
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:Thank you. This makes sense. Regards, GrahamHi 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?
Jun 08 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, GrahamCurious. in std.array, for string types front is defined as dchar front(A)(A a); ick. unicode.
Jun 07 2010