I'm having difficulty understanding how curried functions (with one argument) differ from normal methods. I tried to implement the latter with the former, but wasn't able to.
I have Market trait defined as follows:
package market {
trait Market {
def getBuyRate(currency: String): Double
def getSellRate(currency: String): Double
}
}
I have another FakeMarket trait that extends Market where I wanted to use currying to implement getBuyRate and getSellRate, as follows:
package market {
trait FakeMarket extends Market with Iterable[Double] {
def getRate(factor: Double)(currency: String): Double = {
factor * this.iterator.next()
}
def getBuyRate = getRate(1) _
def getSellRate = getRate(1.02) _
}
}
Finally, I have RandomFakeMarket object that extends FakeMarket:
package market {
object RandomFakeMarket extends FakeMarket {
def iterator = new Iterator[Double] {
def hasNext = true
def next = 100.0
}
}
}
Having the types defined as such gives an error saying:
<console>:10: error: object creation impossible, since:
it has 2 unimplemented members.
/** As seen from object RandomFakeMarket, the missing signatures are as follows.
* For convenience, these are usable as stub implementations.
*/
def getBuyRate(currency: String): Double = ???
def getSellRate(currency: String): Double = ???
object RandomFakeMarket extends FakeMarket {
^
This seems odd to me because FakeMarket implements methods called getBuyRate and getSellRate of String => Double type.
I can get this to work if in Market I had done:
getBuyRate: String => Double
Why is this different from how I originally defined getBuyRate? Why should this be different when there's only one argument? Now it seems that the parent trait Market has to worry about how getBuyRate gets implemented (a normal function vs a curried function).