ダックタイピングに使用するタイプがあります。
type t={
def x:Int
...
}
class t2 {
def x:Int=1
}
def myt:t=new t2 //ducktyping
タイプをインターフェースするように強制されるトレイトを書きたいのですが、これは機能しません:
trait c extends t { //interface DOES NOT COMPILE
def x:Int=1
}
一方、タイプtの代わりにトレイトt1を書くと、ダックタイピング機能が失われます。
trait t1 {
def x:Int
}
type t=t1
trait c extends t1 { // t1 can be used as interface
def x:Int=1
}
def myt:t=new t2 // DOES NOT COMPILE since t1 is expected
では、ダックタイピングとインターフェースの両方をどのように使用できますか?