我有以下地图扩展
object ImplicitMap {
implicit def extendMap(m : Map[String,Any]) = new MapExtension(m)
}
class MapExtension(m : Map[String,Any]) {
def +?(conditional:Boolean)(pair:(String,Any)):Map[String,Any] =
if (conditional) m + pair
else m
}
此函数在使用时会给出Boolean does not take parameters的编译时错误,但是书面显式测试(如下)可以正常工作
test ("Map +?") {
def +?(conditional:Boolean)(pair:(String,Any)):Map[String,Any] = if (conditional) Map.empty + pair else Map.empty
+?(true)("hi" -> 2) should equal (Map("hi" -> 2))
}