Is this a bug?

Christopher Nicholson-Sauls ibisbasenji at gmail.com
Sun Jan 9 21:09:26 PST 2011


On 01/09/11 16:28, Sean Eskapp wrote:
> This code works fine:
> 
> 	int[] arr = [ 1, 2, 3, 4, 5 ];
> 	auto myMax = function int(int a, int b) { return (a > b) ? a : b; };
> 	auto biggest = reduce!(myMax)(arr);
> 
> But passing the function literal directly to reduce causes an error. Is this
> intentional?

I believe this is because binaryFun (which reduce uses) takes a template
alias parameter, which must refer to something bound -- aka, having a
name.  There may be a way around this, to enable using function/delegate
literals, but I'm not immediately sure how or if it's needed.  So, yes,
it is intentional in-so-far as this is how the language feature
(template alias parameters) behaves.

In the meantime, you can use the string way (so long as 'a' and 'b' are
your only variables):
auto biggest = reduce!`(a > b) ? a : b`( arr );

Or you can use the max() function from std.algorithm if your actual use
case isn't more interesting than this.

-- Chris N-S


More information about the Digitalmars-d mailing list