我试图在测试用例中调用隐式函数,但似乎 eclipse UI 无法识别它,我收到编译错误,使用 Eclipse - Build id: 2.1-M2-20121018-1623-Typesafe Eclipse SDK Version: 3.7。 2
我错过了什么吗?
代码
package com.scala.dsl
object TradeDSL {
abstract class Instrument(name: String) { def instrumentName: String }
case class Stock(name: String) extends Instrument(name) {
override val instrumentName = "EQUITY";
}
case class Bond(name: String) extends Instrument(name) {
override val instrumentName = "BOND";
}
abstract class TransactionType { def value: String}
case class Buy() extends TransactionType {
override val value = "BUY"
}
case class Sell() extends TransactionType {
override val value = "SELL"
}
class PricingStrategy(order: Order) {
def defaultPricing(): Int = order.quantity * order.price
def brokeragePricing(): Int = order.quantity * order.price + 100
}
implicit def orderPricing(order: Order) = new PricingStrategy(order);
case class Order(price: Int = 0, instrument: Instrument = null, quantity: Int = 0, totalValue: Int = 0,trn: TransactionType = null, account: String = null ) {
def maxUnitPrice(p: Int) = copy(price = p)
def to(i: Tuple2[Instrument, Int] ) = copy(instrument = i._1, quantity = i._2)
def buy(qi: Tuple2[Int, Instrument]) = copy(instrument = qi._2, quantity = qi._1, trn = Buy())
def sell(qi: Tuple2[Int, Instrument]) = copy(instrument = qi._2, quantity = qi._1, trn = Sell())
def using(pricing: (Int, Int) => Int) = {
copy(totalValue = pricing(quantity, price))
}
def forAccount(a: String)(implicit pricing: (Int,Int) => Int) = {
copy(account = a, totalValue = pricing(quantity,price))
}
}
}
测试用例
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import com.scala.dsl._
import com.scala.dsl.TradeDSL.Bond
import com.scala.dsl.TradeDSL.Stock
import com.scala.dsl.TradeDSL.Buy
import com.scala.dsl.TradeDSL.Sell
import com.scala.dsl.TradeDSL.Order
@RunWith(classOf[JUnitRunner])
class TestTradeDSL extends FunSuite {
test("Order") {
val order1 = new Order()
.buy(10, Stock("GOLD"))
.maxUnitPrice(25)
.brokeragePricing
}
}
在 .brokeragePricing 出现编译错误我尝试清理项目并重新启动 Eclipse,但一切都是徒劳的。