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

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

``val outer = new Outerval inner =  new outer.Inner``

``val outer = new Outerval inner: outer.Inner =  new outer.Inner``

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

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

``val o1 = new Outerval o2 = o1val i1: o1.Inner = new o1.Innerval i2: o2.Inner = new o1.Inner   // 編譯錯誤，type mismatch``

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

``val o = new Outerclass Some extends o.Innerval oi: Outer#Inner = new Some``

``class Foodclass 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 Catval cat2 = new Catcat1.eat(new cat1.F)  // 吃魚cat2.eat(new cat2.F)  // 吃魚cat1.eat(new cat2.F)  // 吃魚cat2.eat(new cat1.F)  // 吃魚``

``type F1 = Fishtype F2 = Fishcat1.eat(new F1)   // 吃魚cat1.eat(new F2)   // 吃魚``

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