是否可以自动' /隐含地丰富匿名函数?

时间:2014-12-16 23:28:42

标签: scala

假设:

enum Car { Mustang, Camaro, Challenger }
enum Driver { Bob, John, Mike }

trait Config {
  def car:Car
  def driver:Driver
  def collect[O](f:PartialFunction[(Car,Driver),O]):O
}

def canDrive(config:Config):Boolean = config collect {
  //what should 'collect' accept in order to satisfy the following:
  //  (1) maintain the terseness
  //  (2) always have access to values of Car and Driver enums
  case (Mustang, Bob) => false
  case (Camaro, Mike) => false
  case _ => true
}

我认为所有枚举值都可以隐藏在一个单独的特征中:

trait AllEnumValues {
  val (Mustang, Camaro, Challenger) = (Car.Mustang, Car.Camaro, Challenger)
  val (Bob, John, Mike) = (Driver.Bob, Driver.John, Driver.Mike)
}

但是,我如何将arg丰富到Config.collect,以便在创建匿名部分函数时,我可以访问AllEnumValues 内的所有内容,而无需隐式导入语句

0 个答案:

没有答案