2016-08-13 17 views
1

...単純型不一致エラー

trait Stream[+A] { 

    def uncons: Option[(A, Stream[A])] 

    def isEmpty: Boolean = uncons.isEmpty 

    def toList[A]: List[A] = this.uncons match { 
    case Some((h,t)) => h::t.toList 
    case None => List() 
    } 

} 

しかし、これは、次のエラーが発生:

type mismatch; found : x$1.type (with underlying type A) required: A 

なぜこのコード私は理解していません動作しません。おそらく、私はこの問題は、あなたのtoListメソッド定義である非常に明白なもの:(

答えて

5

をしないのです これにより:。。

def toList[A]: List[A] = this.uncons match { ... } 

をあなたが実際にあなたの方法がパラメータ化されている新しいタイプAを定義している だけメソッドを宣言:

def toList: List[A] = this.uncons match { ... } 

、あなたが(この定義は、クラスで定義された同じAを使用されるだろう)行ってもいいです

+0

ああ...ありがとう、愚かな間違い:) – Maciej