我爲什麼可以做以下(爲什麼它編譯):走樣適當類型爲生存型
class A
type M[_] = A
我希望我只能別名類型的期望一種類型的參數,例如List[_]
,但它也適用於普通課程。
如果我創建了一個方法:
def foo(m: M[_]) = m
,並用錯誤的參數調用它:
scala> foo("a")
<console>:15: error: type mismatch;
found : String("a")
required: M[_]
(which expands to) A[]
foo("a")
我得到這樣的錯誤。 A[]
是什麼意思?
的進一步深入,這怎麼解釋:
scala> type M[_, _] = A
<console>:12: error: _ is already defined as type _
type M[_, _] = A
有沒有一種方法,以確保我把我的別名的右側將是一個參數化類型?
你的第二個問題:是'A類[T <:AnyRef,U <:AnyVal]'&'類型M = A [_,_]'你想要什麼? – Samar
@Samar這是一個非常不同的情況。 –