【发布时间】:2015-04-18 10:18:39
【问题描述】:
下面的代码运行良好
List("ios","android","wm").exists(x =>"ios ok".contains(x))
但是,如果我像这样在匿名函数中添加参数类型,它会抱怨type mismatch:
scala> List("ios","android","wm").exists(x: String => "ios ok".contains(x))
<console>:1: error: identifier expected but string literal found.
List("ios","android","wm").exists(x: String => "ios ok".contains(x))
^
如果我使用_ 而不是x,它也不会编译:
scala>List("ios","android","wm").exists(_ =>"ios ok".contains(_))
<console>:8: error: missing parameter type for expanded function ((x$2) => "ios ok".<contains: error>(x$2))
有人对此有想法吗? 这些代码中是否发生了任何隐式类型转换? 我怎么能在这里明确地使用参数类型呢?
【问题讨论】:
-
在帖子中包含错误
-
如果你使用
exists((x: String) =>"ios ok".contains(x)),它就可以工作。我假设它正在尝试解析函数类型,例如x: String => SomeType
标签: scala generics lambda functional-programming type-inference