【发布时间】:2019-03-10 16:08:15
【问题描述】:
我有一个复杂的玩具算法,我希望纯粹在类型级别表示:根据饮食要求为当天的一道菜选择修改。为卷积道歉,但我认为我们需要每一层才能到达我想要使用的最终界面。
我的代码存在问题,如果我们在基于另一个泛型类型的Aux-pattern-generated 类型上表达类型约束,则类型推断失败。
这些是餐点,实际上会有很多种比萨饼和多种基础餐点:
trait Pizza
trait CheeselessPizza extends Pizza
饮食要求:
sealed trait DietaryRequirement
trait Vegan extends DietaryRequirement
当日菜式分类:
sealed trait DishOfTheDay[Meal]
object DishOfTheDay {
implicit val dishOfTheDay: DishOfTheDay[Pizza] = null
}
这将改变每天的膳食,独立于程序的其余部分。
ModifiedMeal 类型类,它接受膳食和饮食要求并生成满足要求的submeal。子类型在这里很重要:
// <: Meal is important
sealed trait ModifiedMeal[Meal, D <: DietaryRequirement] { type Mod <: Meal }
object ModifiedMeal {
type Aux[Meal, D <: DietaryRequirement, Mod0 <: Meal] = ModifiedMeal[Meal, D] { type Mod = Mod0 }
// Only one instance so far, Vegan Pizza = CheeselessPizza
implicit val veganPizzaModifiedMeal: ModifiedMeal.Aux[Pizza, Vegan, CheeselessPizza] = null
}
这是为我们进行计算的最终类型类:
// Given a dietary requirement, give us a dish of the day which satisfies it
// if one exists
trait DishOfTheDayModification[Req <: DietaryRequirement] { type Out }
object DishOfTheDayModification {
type Aux[Req <: DietaryRequirement, Out0] = DishOfTheDayModification[Req] { type Out = Out0 }
// Find the dish of the day, then find a ModifiedMeal of it
// <: Meal is important here so we pick up ONLY pizzas and not some other meal
implicit def dishOfTheDayModification[Meal, Req <: DietaryRequirement, Mod <: Meal](
implicit d: DishOfTheDay[Meal],
impl: ModifiedMeal.Aux[Meal, Req, Mod]
): DishOfTheDayModification.Aux[Req, Mod] = null
}
这是测试:
object MealTesting {
def veganDishOfTheDay[Mod](implicit d: DishOfTheDayModification.Aux[Vegan, Mod]): Mod = ???
// Does not compile but it should
veganDishOfTheDay: CheeselessPizza
}
问题是调用这个方法不能编译,但它应该。
如果你复制整个程序但从生成的餐中删除<: Meal 要求,它会编译。这又是整个事情,但“工作”:
trait Pizza
trait CheeselessPizza extends Pizza
sealed trait DietaryRequirement
trait Vegan extends DietaryRequirement
sealed trait DishOfTheDay[Meal]
object DishOfTheDay {
implicit val dishOfTheDay: DishOfTheDay[Pizza] = null
}
sealed trait ModifiedMeal[Meal, D <: DietaryRequirement] { type Mod }
object ModifiedMeal {
type Aux[Meal, D <: DietaryRequirement, Mod0] = ModifiedMeal[Meal, D] { type Mod = Mod0 }
implicit val veganPizzaModifiedMeal: ModifiedMeal.Aux[Pizza, Vegan, CheeselessPizza] = null
}
trait DishOfTheDayModification[Req <: DietaryRequirement] { type Out }
object DishOfTheDayModification {
type Aux[Req <: DietaryRequirement, Out0] = DishOfTheDayModification[Req] { type Out = Out0 }
implicit def dishOfTheDayModification[Meal, Req <: DietaryRequirement, Mod](
implicit d: DishOfTheDay[Meal],
impl: ModifiedMeal.Aux[Meal, Req, Mod]
): DishOfTheDayModification.Aux[Req, Mod] = null
}
object MealTesting {
def veganDishOfTheDay[Mod](implicit d: DishOfTheDayModification.Aux[Vegan, Mod]): Mod = ???
// DOES compile
veganDishOfTheDay: CheeselessPizza
}
但我们不希望这样,因为它允许我们生成不是当天菜肴的子类型的菜肴。
有谁知道为什么Aux 模式中的继承会导致失败,或者我可以如何使用中间隐式构造程序来尝试解决问题?
【问题讨论】:
标签: scala types type-level-computation