rookie
rookie

Reputation: 7883

problem in generic function

enter code herehello, everyone, can somebody explain what is my problem in this function:

public class Summer{
 public <X,Y> Y[] sum(X[] inArr, Y first, SumFunction<Y,X> f, Y[] outArr){
  for(int i = 0; i < inArr.length; i++){
   outArr[i] = f.op(inArr[i], first); //here I have problem
   first = outArr[i];
  }
  return outArr;
 }
}

I receive an error:

The method op(Y, X) in the type SumFunction<Y,X> is not applicable for the arguments (X, Y)

I need to use this function, how can I do this, thanks for any suggestions

Upvotes: 0

Views: 82

Answers (4)

Paulo Guedes
Paulo Guedes

Reputation: 7279

It seems like the op() function is expecting f.op(first, inArr[i]) instead

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114797

You're using

f.op(X value1, Y value2);

Double check if this matches the declaration SumFunction#op. It looks like, SumFunction#op expects the arguents in a different order.

Upvotes: 0

Yuriy Tkach
Yuriy Tkach

Reputation: 78

My quick guess would be that the method op in SumFunction has the following signature:

Y op(Y, X);

But you first pass the X argument and then the Y. So, revert arguments in the method call, so it would be:

outArr[i] = f.op(first, inArr[i]);

However, it would be better, if you posted the code for the SumFunction class.

Upvotes: 0

sly7_7
sly7_7

Reputation: 12011

I think you have to call f.op(first, inArray[i]). The op() method of SumFunction seems to take an Y as the first argument, and an X as the second.

Upvotes: 1

Related Questions