我具有以下特征:类型参数的类型不匹配
sealed trait Tree[+A]
case class Leaf[A](value: A) extends Tree[A]
case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]
然后我定义一个变量与Branch
类型:上述
val t = Branch(Branch(Leaf("S"), Leaf(2)), Leaf(99))
代码工作正常。但是,当我将其更改为:
val t = Branch[Int](Branch(Leaf("S"), Leaf(2)), Leaf(99))
编译器会抱怨:
Error:(41, 37) type mismatch;
found : String("S")
required: Int
val t = Branch[Int](Branch(Leaf("S"), Leaf(2)), Leaf(99))
当我确定第一Branch
类型(在这种情况下为int),那么节点将固有的家长吗?
因此,当我不强制使用适当的类型时,任何类型的'Any'都会被使用。 –
不,第一种常见的类型将被使用。在这种情况下,它是“任何”。 –
当我强制类型是一个'Int',那么所有类型的节点和叶子将是'Int'?它会从分支固有吗? –