# 路徑相依型態（Path-dependent type）

class Outer {
private val x = 10
class Inner {
private val y = x + 10
}
}

val outer = new Outer
val inner = new outer.Inner

val outer = new Outer
val inner: outer.Inner = new outer.Inner

outer.Inner這樣的型態，稱之為路徑相依型態（Path-dependent type），所謂路徑（Path），指的是參考住外部類別所建立實例的名稱，就outer.Inner這個型態來說，其路徑為outer。之所以稱之為路徑相依型態，是因為不同的路徑，就代表著不同的型態。例如：
val o1 = new Outer
val o2 = new Outer
val i1 = new o1.Inner
val i2 = new o2.Inner

val o1 = new Outer
val o2 = new Outer
val i: o2.Inner = new o1.Inner // 編譯錯誤，type mismatch

val o1 = new Outer
val o2 = o1
val i1: o1.Inner = new o1.Inner
val i2: o2.Inner = new o1.Inner // 編譯錯誤，type mismatch

val o1 = new Outer
val o2 = new Outer
val i1: Outer#Inner = new o1.Inner
val i2: Outer#Inner = new o2.Inner

val o = new Outer
class Some extends o.Inner
val oi: Outer#Inner = new Some

class Food

class Fish extends Food {
override def toString = "魚"
}

abstract class Animal {
type F <: Food
def eat(f: F)
}

class Cat extends Animal {
type F = Fish
def eat(fish: Fish) {
println("吃" + fish)
}
}

val cat1 = new Cat
val cat2 = new Cat
cat1.eat(new cat1.F) // 吃魚
cat2.eat(new cat2.F) // 吃魚
cat1.eat(new cat2.F) // 吃魚
cat2.eat(new cat1.F) // 吃魚

type F1 = Fish
type F2 = Fish
cat1.eat(new F1) // 吃魚
cat1.eat(new F2) // 吃魚

val cat = new Cat
cat.eat(new Cat#F) // 吃魚