假设:
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
内的所有内容,而无需隐式导入语句