Major редактировать, чтобы лучше отразить мое намерение:Предотвращение нежелательных комбинаций с использованием зависимых от пути типов?
// this *must* be outside of Team
class Player
class Team(val p1: Player, val p2: Player) {
type P = ??? // <-- what to put here ? see below ...
// perhaps I can somehow construct a union type:
// type P = p1.type union p2.type
// or perhaps the following :
// type P = Either[p1.type,p2.type]
// The purpose of `P` here is to define a type which can only hold p1 or p2 for values.
def position(p: P) = p match {
case `p1` => 1
case `p2` => 2
// Gotcha ! we never get here (=exactly what I want to enforce) if I can get the `P` right !
}
}
val p1=new Player
val p2=new Player
val someP = new Player
val t= new Team(p1,p2)
t.position(t.p1) // allowed
t.position(t.p2) // allowed
t.position(p1) // allowed, since p1 is the same thing as t.p1, or is it ?!
t.position(p2) // allowed, since p2 is the same thing as t.p2, or is it ?!
t.position(someP) // I want this to *not* type-check : Required t.P, Found Player
Оригинальный фрагмент здесь:
// this *must* be outside of Team
class Player
class Team(p1: Player, p2: Player) {
def position(p: Player) = p match {
case `p1` => 1
case `p2` => 2
// Ah-oh !! is it possible to prevent this using (probably path-dependent) types ?
}
}
val p1=new Player
val p2=new Player
val someP = new Player
val t= new Team(p1,p2)
t.position(p1) // allowed
t.position(p2) // allowed
t.position(someP) // I want this to *not* type-check with an error like "Required: t.Player, Found: Player"
Что делать вы имеете в виду типы, зависящие от пути? 't.position (someP)' всегда будет проходить проверку типа cos 'someP' от ожидаемого типа Player. Вы можете генерировать исключение в 'position', например' case _ => throw InvalidPlayerException' –
. T.position (someP) всегда будет передавать проверку типа cos someP от ожидаемого типа. Игрок «Я знаю, и это точно проблема. Хотя я вижу путаницу ... Я отредактирую вопрос, чтобы лучше отразить мои намерения. –