在scala中看到一个的函数定义:
def execute(sql: String) (f: PreparedStatement => Unit)
这种语法叫作**参数curry化**,即把函数的参数分开写。它等价于:
````def execute(sql: String, f: PreparedStatement => Unit)
即第一个参数是一个String,第二个是一个函数定义:输入PreparedStatement,得到Unit
<span id="more-346"></span>
之所有要把它分开写,是因为在scala中,`()`可以写作`{}`,我们在调用时,可以写得好看一些:
````execute("select* from users") { ps: PreparedStatement => // do something with the ps }
再来一个例子,对于这个函数:
````def exec(s1:String)(s2:String)(s3:String)
可以这样调用:
````exec("s1")("s2")("s3") exec("s1")("s2"){"s3"} exec("s1"){"s2"}{"s3"} exec{"s1"}{"s2"}{"s3"} exec{"s1"}("s2"){"s3"}
注意里面的()和{},任意一个都可以互换的。
当然,我们通常在参数内容比较长的时候,才用`{}`,并且分行写:
````exec("s1")("s2"){ val s3 = "sdfdsfdsfdsfsdfsdfsdfdsf" s3 + "rrrrrrrr" }