www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Explicitly Instantiated Function Template won't work as Array

reply HOSOKAWA Kenchi <hskwk inter7.jp> writes:
Moritz Warning Wrote:

 On Sat, 25 Oct 2008 02:19:05 -0400, HOSOKAWA Kenchi wrote:
 
 import std.algorithm;
 [0,1,2,3].reverse!(int[]);  // fails
 [0,1,2,3].reverse;  // works
 
 
 Please let me know the former expression fails due to the language
 limitation or not.
 
 
 thanks,

http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#reverse reverse is no template.

std.algorithm.reverse is a Function template. definition of std.algorithm.reverse is : void reverse(alias iterSwap = .iterSwap, Range)(Range r); thus it is a template, according to "Function Templates" in http://www.digitalmars.com/d/2.0/template.html please note these codes are working (and failing) in dmd 2.020 int[] arr = [ 1, 2, 3 ]; reverse!(std.algorithm.iterSwap, int[])(arr); // works. arr.reverse!(std.algorithm.iterSwap, int[]); // fails assert(arr == [ 3, 2, 1 ]);
Oct 27 2008
parent "Denis Koroskin" <2korden gmail.com> writes:
On Mon, 27 Oct 2008 11:47:39 +0300, HOSOKAWA Kenchi <hskwk inter7.jp>  
wrote:

 Moritz Warning Wrote:

 On Sat, 25 Oct 2008 02:19:05 -0400, HOSOKAWA Kenchi wrote:

 import std.algorithm;
 [0,1,2,3].reverse!(int[]);  // fails
 [0,1,2,3].reverse;  // works


 Please let me know the former expression fails due to the language
 limitation or not.


 thanks,

http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#reverse reverse is no template.

std.algorithm.reverse is a Function template. definition of std.algorithm.reverse is : void reverse(alias iterSwap = .iterSwap, Range)(Range r); thus it is a template, according to "Function Templates" in http://www.digitalmars.com/d/2.0/template.html please note these codes are working (and failing) in dmd 2.020 int[] arr = [ 1, 2, 3 ]; reverse!(std.algorithm.iterSwap, int[])(arr); // works. arr.reverse!(std.algorithm.iterSwap, int[]); // fails assert(arr == [ 3, 2, 1 ]);

Oh, that's easy: array should be the first parameter in a function, it won't work as a pseudo-member function otherwise.
Oct 27 2008