0

I have the below function in scala:

 def addAllCosts(costs: List[Int],discount: Int => Int): Int = {
var sum = 0
costs.foreach(sum += _)
discount(sum)
}

I am invoking the function like so in a http akka router:

HttpResponse(200, entity= repository.addAllCosts(costs,repository.applyDiscount(23)))

applyDiscount looks like this:

def applyDiscount(sum:Int): Int = {
    return sum - discount
  }

However, I am getting the following error:

Error:(45, 94) type mismatch;
 found   : Int
 required: Int => Int

Not sure how to resolve this? Thanks!

1 Answer 1

1

applyDiscount is an Int => Int function, however, you're passing repository.applyDiscount(23) as the value of the discount argument, and that expression has the type Int (because it's the result of applying the function to the value 23), instead of the expected type Int => Int.

Not sure where the value 23 comes from, but this should at least compile of you just pass a reference to the method without applying it:

HttpResponse(200, entity= repository.addAllCosts(costs, repository.applyDiscount))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.