我有一个带有两个输入的simpleNode类,你只能填充其中一个,这些都是Scala中的Map,但我必须检查地图中的数据类型才能填充任何输入
我写的代码是:
class SimpleNode (
val uriTriples: collection.mutable.Map[String, List[String]] = collection.mutable.Map.empty,
val valueTriples: collection.mutable.Map[String, Map[String,String]] = collection.mutable.Map.empty
)
{
def this(map:collection.mutable.Map) = {
map.values.head match {
case uri : List[String] => this(uris,null)
case values : Map[String,String] => this(null,values)
case _=>
}
}
}
我总是面对错误:
a:34: error: 'this' expected but identifier found.
[INFO] map.values.head match {
[INFO] ^
答案 0 :(得分:3)
消除歧义的常用策略:
class SimpleNode (
val uriTriples: collection.mutable.Map[String, List[String]] = collection.mutable.Map.empty,
val valueTriples: collection.mutable.Map[String, Map[String,String]] = collection.mutable.Map.empty
)
{
def this(map:mutable.Map[String, List[String]]) = this(map, null)
def this(map:mutable.Map[String, Map[String,String]])(implicit d: DummyImplicit) = this(null, map)
}
或者工厂更加行人:
object SimpleNode {
def apply(...) = ???
}