简体   繁体   English

如何使用显式返回类型定义Scala闭包

[英]How to define Scala closure with explicit return type

Why doesn't this work in scala: 为什么这在scala中不起作用:

val cloz: (Int,String => String) = (num: Int, str: String) => {
    str+"-"+num
}

I see a few examples of closures being defined with only 1 arg, like this: 我看到一些仅用1 arg定义的闭包示例,如下所示:

val thingy: (Int => Int) = (num: Int) => {
    num * 2
}

But absolutely nowhere (including Scala ebooks) could I find any information explaining the syntax of "val" closures. 但是我绝对找不到任何地方(包括Scala电子书)能够解释“ val”闭包语法的任何信息。

Thanks! 谢谢! Jamie 杰米

The correct syntax is: 正确的语法是:

val cloz: (Int, String) => String = (num: Int, str: String) => {
    str + "-" + num
}

By the way, in this simple case you can also simplify expression like this (especially if you already explicitly specifying the type of the function): 顺便说一下,在这种简单的情况下,您还可以简化这样的表达式(尤其是如果您已经明确指定了函数的类型):

val cloz: (Int, String) => String = (num, str) => str + "-" + num 

Update 更新资料

You can also use REPL to explore Scala - it's very nice tool. 您还可以使用REPL探索Scala-这是一个非常不错的工具。 You can start it just by starting scala without any arguments. 您可以通过不带任何参数的scala来启动它。 Here is example session: 这是示例会话:

scala> val cloz = (num: Int, str: String) => str + "-" + num
cloz: (Int, String) => java.lang.String = <function2>

scala> val cloz: (Int, String) => String = (num: Int, str: String) => {
     |     str + "-" + num
     | }
cloz: (Int, String) => String = <function2>

scala> val cloz: (Int, String) => String = (num, str) => str + "-" + num
cloz: (Int, String) => String = <function2>

scala> def printCloz(cloz: (Int, String) => String, num: Int, str: String) = print(cloz(num, str))
printCloz: (cloz: (Int, String) => String, num: Int, str: String)Unit

As you can see it not only allows you to interactively execute code, but also prints type information if you define something. 如您所见,它不仅允许您交互式执行代码,而且在定义内容时也可以打印类型信息。

Based on the excellent answer from @Eazy Angel, I offer this for anyone who's confused as I was: 基于@Eazy Angel的出色回答,我向像我一样困惑的任何人提供此服务:

val cloz: (Int,String) => String = (num, str) => {
    str+"-"+num
}
def f1(  clozArg: ((Int,String) => String), intArg: Int, stringArg: String  ): String = {
    clozArg(intArg,stringArg)
}
println("f1 result="+f1(cloz, 5, "okee"))

Please note however (please correct me if I'm wrong) that b/c of Scala's type inference you are not required to specify all these types explicitly. 但是请注意(如果我错了,请纠正我)Scala类型推断的b / c不需要显式指定所有这些类型。 The only time I have seen so far that you must do is when using recursion. 到目前为止,我唯一必须看到的就是使用递归时。

-- -
UPDATE: 更新:
This bizarre syntax works also: (see return type on 3rd line) 这种 奇怪的 语法也可以使用:(请参见第三行的返回类型)

val cloz = (num: Int, str: String) => {
    str+"-"+num
} : String
def f1(  clozArg: ((Int,String) => String), intArg: Int, stringArg: String  ): String = {
    clozArg(intArg,stringArg)
}
println("f1 result="+f1(cloz, 5, "okee"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM