Milk
Milk

Reputation: 103

Scala how to reference a method that with an implicit parameter

The method have a implicit parameter can not be reference as argument ? In my code ,i create a method that have a implicit parameter. Some time i just want to transfer it to other method . In this time Scala give me error. See below:

case class ComplicatedSalesTaxData(baseRate: Float,isTaxHoliday: Boolean) 
def calcText(amount: Float,rate : (ComplicatedSalesTaxData) => Float ) : Float = amount * rate(ComplicatedSalesTaxData(0.06F,false))
def rate(implicit cstd:ComplicatedSalesTaxData) = {
if(cstd.isTaxHoliday) 
    cstd.baseRate
else 
    0.01F }

calcText(100F,rate) // will get error : could not find implicit value for parameter cstd: ComplicatedSalesTaxData

Upvotes: 0

Views: 382

Answers (2)

jan0sch
jan0sch

Reputation: 52

The error message that you have posted says that the compiler cannot find an implicit ComplicatedSalesTaxData in the current scope. Therefore you have to define one. Then the call should look like this calcText(100F,rate(_)) instead of the wildcard _ you can also pass the value explictely.

Upvotes: 0

Cyrille Corpet
Cyrille Corpet

Reputation: 5305

You have to say that you want to pass the parameter explicitly:

calcText(100F,rate(_)) 

Upvotes: 2

Related Questions