scala 匿名函数缺少参数类型错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13115053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:40:53  来源:igfitidea点击:

scala anonymous function missing parameter type error

scala

提问by Qiang Li

I wrote the following

我写了以下内容

def mapFun[T, U](xs: List[T], f: T => U): List[U] = (xs foldRight List[U]())( f(_)::_ )

and when I did

当我这样做的时候

def f(x: Int):Int=x*x
mapFun(List(1,2,3), f)

It worked fine. However, I really wanted to make the following work too

它工作得很好。但是,我真的很想做以下工作

mapFun(List(1,2,3), x=>x*x)

It complains about "missing parameter type". I know that I could use currying, but is there any way to still use anonymous function for non-currying def I had above?

它抱怨“缺少参数类型”。我知道我可以使用柯里化,但有没有什么办法仍然使用匿名函数来实现我上面提到的非柯里化定义?

回答by wleao

It seems to me that because "f" is in the same parameter list as "xs", you're required to give some information regarding the type of x so that the compiler can solve it.

在我看来,因为“f”与“xs”在同一个参数列表中,你需要提供一些关于 x 类型的信息,以便编译器可以解决它。

In your case, this will work:

在您的情况下,这将起作用:

mapFun(List(1,2,3) , (x: Int) => x * x)  

Do you see how I'm informing the compiler that x is an Int?

你看到我是如何通知编译器 x 是一个 Int 的吗?

A "trick" that you can do is currying f. If you don't know what currying is check this out: http://www.codecommit.com/blog/scala/function-currying-in-scala

你可以做的一个“技巧”是柯里化 f。如果您不知道什么是咖喱,请查看:http: //www.codecommit.com/blog/scala/function-currying-in-scala

You will end up with a mapFun like this:

你最终会得到一个像这样的 mapFun:

def mapFun[T, U](xs: List[T])(f: T => U): List[U] = 
    (xs foldRight List[U]())( f(_)::_ )

And this will work:

这将起作用:

mapFun(List(1,2,3))(x => x * x)

In the last call, the type of x is resolved when the compiler checks the first parameter list.

在最后一次调用中,当编译器检查第一个参数列表时解析 x 的类型。

EDIT:

编辑:

As Dominic pointed out, you could tell the compiler what your types are. Leading to:

正如 Dominic 所指出的,您可以告诉编译器您的类型是什么。导致:

mapFun[Int, Int](List(1,2,3), x => x * x)

Cheers!

干杯!

回答by Ratan Sebastian

The limitation of scala's type system that you're running into here is that the type information flows from left to right across parameter groupsand does not flow from left to right within a parameter group.

您在这里遇到的 scala 类型系统的限制是类型信息从左到右跨参数组流动,而不是在参数组内从左到右流动。

What this means is that specifying the type parameter Tby providing a List[Int]will not provide that information to other parameters within the group like f. Which results in a missing parameter type error. But it will provide it to fif f were a part of the next parameter group. This is why the curried function approach works.

这意味着T通过提供 a来指定类型参数List[Int]不会将该信息提供给组内的其他参数,如f. 这会导致缺少参数类型错误。但是f如果 f 是下一个参数组的一部分,它将提供它。这就是柯里化函数方法起作用的原因。

i.e. if you defined it like this:

即如果你这样定义它:

def mapFun[T, U](xs: List[T])(f: T => U): List[U] = (xs foldRight List[U]())( f(_)::_ )

The type parameter Tthat you define in the first parameter group: (xs: List[T])as Intwill be made available to the next parameter group: (f: T => U). So now you do not have to explicitly specify Tat the call site.

T您在第一个参数组中定义的类型参数:(xs: List[T])asInt将可用于下一个参数组:(f: T => U)。所以现在您不必T在调用站点上明确指定。