不推断的多参数闭包参数类型 [英] Multiple parameter closure argument type not inferred

查看:134
本文介绍了不推断的多参数闭包参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,我不能以我想要的方式表现。我有一个类以下列方式定义(为此剥离):

I have a piece of code that I can't get to behave in the way I'd like. I have a class defined in the following way (stripped down for this):

class Behaviour[T](private val rule: Time => T) {
  def map1[U, V](behaviour: Behaviour[U], func: (T, U) => V): Behaviour[V] = {
    new Behaviour(time => func(this.at(time), behaviour.at(time)))
  }
}

在玩这个类的时候,我尝试了一些我认为不重要的东西:

When playing around with this class I tried to something that I thought would be trivial:

val beh = Behaviour(time => 5)
val beh2 = Behaviour(time => 5)
beh.map1(beh2, (a, b) => a + b)

对于最后一行,我收到以下错误:

For the last line I receive the following error:

<console>:13: error: missing parameter type
          beh.map1(beh2, (a, b) => a + b)
                             ^

我当然可以指定closure参数类型,类型推理工作在这里?当然,我也可以指定函数的泛型类型(见下面的两个解决方案)。

I can of course specify the closure parameter types and it works correctly but why doesn't type inference work here? Of course I could also specify the generic types for the function (see below for both solutions).

我想Scala执行了一个'scan'来推断类型,并会看到 beh2 假设 U 这里是 Int 。有没有一些方法我可以解决这个没有指定输入参数的类型(对于闭包或泛型)?

I thought Scala carried out a 'scan' to infer types and would see beh2 and passed into the function and assume U here to be Int. Is there some way I can fix this without specify the types of the input parameters (for the closure or the generics)?

编辑:我有两个修复的示例:

Examples of the two fixes I have:

beh.map1[Int, Int](beh2, (a, b) => a + b)
beh.map1(beh2, (a, b : Int) => a + b)


推荐答案

请参阅 scala-debate 线程讨论这里发生了什么。问题是Scala的类型推理是根据参数列表而不是 参数发生的。

See this scala-debate thread for a discussion of what's going on here. The problem is that Scala's type inference happens per parameter list, not per parameter.

那个线程,目前的方法有一个很好的理由。如果Scala有每参数类型推理,编译器不能推断同一参数列表中的类型的上界。请考虑以下内容:

As Josh Suereth notes in that thread, there's a good reason for the current approach. If Scala had per-parameter type inference, the compiler couldn't infer an upper bound across types in the same parameter list. Consider the following:

trait X
class Y extends X
class Z extends X

val y = new Y
val z = new Z

def f[A](a: A, b: A): (A, A) = (a, b)
def g[A](a: A)(b: A): (A, A) = (a, b)

f(y,z)完全符合我们的预期,但 g (z)给出了类型不匹配,因为在编译器到达第二个参数列表时,已经选择 Y 作为 A

f(y, z) works exactly as we'd expect, but g(y)(z) gives a type mismatch, since by the time the compiler gets to the second argument list it's already chosen Y as the type for A.

这篇关于不推断的多参数闭包参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆