Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function scala-传递一个将另一个函数作为参数的函数_Function_Scala_Parameters_Arguments_Higher Order Functions - Fatal编程技术网

Function scala-传递一个将另一个函数作为参数的函数

Function scala-传递一个将另一个函数作为参数的函数,function,scala,parameters,arguments,higher-order-functions,Function,Scala,Parameters,Arguments,Higher Order Functions,我试图将函数作为参数传递,但该函数有多个参数(其中一个是函数) 下面是我在一个基本Python示例中尝试执行的操作: def first(string1, string2, func): func(string1, string2, third) def second(string1, string2, func): func(string1, string2) def third(string1, string): # operations go here fir

我试图将函数作为参数传递,但该函数有多个参数(其中一个是函数)

下面是我在一个基本Python示例中尝试执行的操作:

def first(string1, string2, func):
    func(string1, string2, third)

def second(string1, string2, func):
    func(string1, string2)

def third(string1, string):
    # operations go here

first("one", "two", second)
我在Scala中的尝试如下:

def first(string1: String, string2: String, func: (Any, Any, Any) => Unit) = {
    func(string1, string2, func)
}
def second(string1: String, string2: String, func: (Any, Any) => Unit) = {
    func(string1, string2)
}

def third(string1: String, string2: String) = {
    // operations
}

def main(args: Array[String]): Unit = {
    first("one", "two", second)
}
val f = second _
val af: (Any, Any, Any) => Unit = f
af(1, "abc", 5)
试图将
第二个
传递到
第一个
时出现错误,参数数量不足。是否有可能以与Python示例相同的样式实现此功能

编辑:

我尝试用
第一个(“一”、“二”、第二个”
替换我的主方法的主体,它给了我一个类型不匹配的错误

类型失配;找到:(字符串,字符串,(任意,任意)=>Unit)=>Unit required:(任意,任意)=> 单位


你知道这里发生了什么吗?

你试图做的不是类型安全的。您不能将
(字符串,字符串,(任意,任意)=>Unit)=>Unit
分配给
(任意,任意)=>Unit
。如果可以,则可以执行以下操作:

def first(string1: String, string2: String, func: (Any, Any, Any) => Unit) = {
    func(string1, string2, func)
}
def second(string1: String, string2: String, func: (Any, Any) => Unit) = {
    func(string1, string2)
}

def third(string1: String, string2: String) = {
    // operations
}

def main(args: Array[String]): Unit = {
    first("one", "two", second)
}
val f = second _
val af: (Any, Any, Any) => Unit = f
af(1, "abc", 5)
如果更精确地指定类型,则可以执行此操作:

def second(string1: String, string2: String, func: (String, String) => Unit) = {
    func(string1, string2)
}

def third(string1: String, string2: String) = {
    // operations
}

def first(string1: String, string2: String, func: (String, String, (String, String) => Unit) => Unit) = {
    func(string1, string2, third)
}

因此,我要做的实际程序有7个函数,它们将另一个函数作为参数,所有这些函数都嵌套在我的示例代码中。我猜在Scala中维护Python程序的风格并不实际?@kevin-是的,传递嵌套回调会很快变得很糟糕。如果你想做一些异步操作,你可以看看未来。否则的话,理解和宏就可以用来简化代码的语法结构。我接受了你的答案,因为它确实解决了我的问题。有没有什么例子可以让我使用宏来提供帮助?@kevin-async/await的Scala实现就是一个很好的例子,请参阅