有人能解释一下这到底发生了什么吗?我现在还没有充分考虑到它:
val s = Seq(1D,2D,3D,4D)
case class WithUnit(value: Double, unit: String)
s map { WithUnit(_,"cm") } // works
s map { WithUnit(value = _ , unit = "cm") } // error: missing parameter type for expanded function ((x$2) => value = x$2)
Run Code Online (Sandbox Code Playgroud)
我想编译器不能推断参数类型,因为我写了参数的名称.但为什么不呢?它只是因为陈述参数的名称而不应该更加困难?!
谢谢!
我有以下代码:
var x = Array(1,3,4,4,1,1,3)
var m = Int.MaxValue
x.foreach((x)=>(m = m min x))
Run Code Online (Sandbox Code Playgroud)
我试图将最后一句简化为:
x.foreach((m = _ min m))
Run Code Online (Sandbox Code Playgroud)
但口译员说:
scala> x.foreach((m = _ min m))
<console>:8: error: missing parameter type for expanded function ((x$1) => x$1.min(m))
x.foreach((m = _ min m))
^
Run Code Online (Sandbox Code Playgroud)
我试图更明确地说明这种类型:
scala> x.foreach((m = (_:Int) min m))
<console>:8: error: type mismatch;
found : (Int) => Int
required: Int
x.foreach((m = (_:Int) min m))
^
Run Code Online (Sandbox Code Playgroud)
编译器和我彼此不了解:(
最好的祝福,
斯坦
syntax lambda scala function-literal scala-placeholder-syntax
看看这些scala片段:如果我们有这样的东西:
List(List(1, 2), List(3, 4), List(5)) map (x => (x.size))
Run Code Online (Sandbox Code Playgroud)
我们可以缩短到:
List(List(1, 2), List(3, 4), List(5)) map ((_.size))
Run Code Online (Sandbox Code Playgroud)
但是,如果我们有这样的事情:
List(List(1, 2), List(3, 4), List(5)) map (x => (x.size, x.size))
Run Code Online (Sandbox Code Playgroud)
为什么我们不能缩短它:
List(List(1, 2), List(3, 4), List(5)) map ((_.size, _.size))
Run Code Online (Sandbox Code Playgroud)
?
在Twitter的Scala学校 集合部分,他们显示了一个具有部分功能的Map作为值:
// timesTwo() was defined earlier.
def timesTwo(i: Int): Int = i * 2
Map("timesTwo" -> timesTwo(_))
Run Code Online (Sandbox Code Playgroud)
如果我尝试用Scala 2.9.1和sbt编译它,我得到以下内容:
[error] ... missing parameter type for expanded function ((x$1) => "timesTwo".$minus$greater(timesTwo(x$1)))
[error] Map("timesTwo" -> timesTwo(_))
[error] ^
[error] one error found
Run Code Online (Sandbox Code Playgroud)
如果我添加参数类型:
Map("timesTwo" -> timesTwo(_: Int))
Run Code Online (Sandbox Code Playgroud)
然后我得到以下编译器错误:
[error] ... type mismatch;
[error] found : Int => (java.lang.String, Int)
[error] required: (?, ?)
[error] Map("timesTwo" -> timesTwo(_: Int))
[error] ^
[error] one error found
Run Code Online (Sandbox Code Playgroud)
我很难过.我错过了什么?
我无法找到一种方法来提供一个空(无操作)方法来完成以下Scala代码中的catch块:
var autoCloseables: List[AutoCloseable] = List()
... //some code that fills the list with various java.sql.* instances; Connection, Statement, ResultSet
autoCloseables.map(try {_.close} catch {case se: SQLException => NoOp} )
Run Code Online (Sandbox Code Playgroud)
我试图NoOp用" ()"," Unit"," None"," se.getMessage()"等替换" " .我继续在Eclipse中收到错误,说明各种形式的"类型不匹配;发现:单位,必需:AutoCloseable =>?".
我也尝试将最后一行改为下面,但仍然收到与上述相同的警告:
autoCloseables.map(try {_.close} catch {case _: Throwable => } )
Run Code Online (Sandbox Code Playgroud)
任何具体的指导将非常感谢.而且,我了解ARM库.现在,请假设我无法使用它,并且需要从这个特定问题构成框架中解决的问题.谢谢.
我正在学习Scala并正在尝试使用命令行.我无法理解为什么第二行无法编译.有人可以解释一下.
val list = List(1,2,3,4)
list.map(_+1) //This works. List(2,3,4,5)
list.map(List(_+1)) //But, Compilation Fails here
list.map(x=>List(x+1)) //This works. List(List(2),List(3),List(4),List(5))
Run Code Online (Sandbox Code Playgroud)
谢谢.