在函数文字的输入参数中使用下划线时接收 "missing parameter type"

Receiving "missing parameter type" when using underscore for input parameter on function literal

我有一个带有通用参数的 trait,其中包含一个我试图将默认实现定义为 "empty" 的方法。

trait MetaBase[T <: Throwable] {
  ...
  def riskWithEvent[V](
    vToEvaluate: => V,
    failureTEvent: FailureBase[T, V] => Unit = _ => ()
  ): TryBase[T, V] =
    ...
}

我在 failureTEvent: FailureBase[T, V] => Unit = 之后的下划线处收到 "missing parameter type" 错误。我无法弄清楚如何让 Scala 编译器在那时不必知道类型信息,因为它不被使用或不需要。

我考虑过将参数更改为:

failureTEvent: Option[FailureBase[T, V] => Unit] = None

但是,我不喜欢客户端现在必须将其函数包装在 Some() 中。我更愿意允许他们不指定参数,或者不使用包装器指定参数。

非常感谢对此的任何指导。

实际上,V 参数有问题。

这里是-Ylog:typer -Ytyper-debug.

|    |    |    |    |    |-- ((x) => ()) : pt=FB[T,?] => Unit BYVALmode-EXPRmode (site: value g in MB) 
<console>:13: error: missing parameter type
       trait MB[T <: Throwable] { def f[V](g: FB[T, V] => Unit = _ => ()): Unit = () }
                                                                 ^
|    |    |    |    |    |    \-> <error> => Unit

或者,

scala> case class FB[T, V](t: T, v: V)
defined class FB

这个有效:

scala> trait MB[T <: Throwable, V] { def f(g: FB[T, V] => Unit = _ => ()): Unit = () }
defined trait MB

这不是:

scala> trait MB[T <: Throwable] { def f[V](g: FB[T, V] => Unit = _ => ()): Unit = () }
<console>:13: error: missing parameter type
       trait MB[T <: Throwable] { def f[V](g: FB[T, V] => Unit = _ => ()): Unit = () }
                                                                 ^

或者只取 Any,因为函数在 arg 中是逆变的:

scala> trait MB[T <: Throwable] { def f[V](g: FB[T, V] => Unit = (_: Any) => ()): Unit = () }
defined trait MB

与默认 arg 键入相关的其他链接:

https://issues.scala-lang.org/browse/SI-8884

https://issues.scala-lang.org/browse/SI-7095

Scala case class.type does not take parameters