考慮下面的代碼理解存在類型斯卡拉
import scala.collection.mutable.Set
import scala.collection.mutable.HashMap
// classes to create field types that do conversion of string to other types
trait Field[A] {
def convert(x: String): A // Need to define convert for the trait, too.
}
case class IntField extends Field[Int] {
override def convert(x: String): Int = x.toInt
}
case class StringField extends Field[String] {
override def convert(x: String): String = x
}
// this function can take any Field type and return a HashMap,
// more important here is type of key not the value of HashMap
// which has to match with value returned from Field.convert()
def someFunc[A](field: Field[A]): HashMap[A, Int] = {
val index = new HashMap[A, Int]()
val data = List("111", "222", "333")
for (line <- data) {
val values: A = field.convert(line)
index.put(values, 0)
}
index
}
// this empty set will be populated with Field objects, and here I get an error
var fields = Set[Field[A]]()
def addField[A](field: Field[A]): Unit = fields += field
addField(StringField())
addField(IntField())
for (field <- fields) println(someFunc(field))
// RESULT
Map(333 -> 0, 222 -> 0, 111 -> 0) // HashMap[String, Int]
Map(111 -> 0, 333 -> 0, 222 -> 0) // HashMap[Int, Int]
以上就不能編譯,錯誤的是not found: type A
創建var fields
時 - 。現在,當我改變這一行改爲:
var fields = Set[Field[A] forSome {type A}]()
一切編譯,但我完全不明白這裏發生了什麼。我知道執行var fields = Set[Field[A]]()
類型A不存在,但其他線路如何解決這個問題,它是甚至適當的解決方案,或者它恰好在這種情況下工作?
的一小段路。如果我簡單地聲明'在代碼的頂部類型A'和使用'變種fields = Set [Field [A]]()'它仍然不能編譯。是因爲'Field [A] for {Some A}'在Field [A]'的上下文中創建了'Type A'?如果這是有道理的。 –
如果你聲明類型是這樣的:type A = Field [B] forSome {type B}它將被編譯,所以A forSome {type A}就是特殊的scala結構,它使你能夠創建存在類型,這就是說它可以是任何類型 – grotrianster