我有一个这样的函数:
def ifSome[B, _](pairs:(Option[B], B => _)*) {
for((paramOption, setFunc) <- pairs)
for(someParam <- paramOption) setFunc(someParam)
}
以及像这样的重载函数:
class Foo{
var b=""
def setB(b:String){this.b = b}
def setB(b:Int){this.b = b.toString}
}
val f = new Foo
然后,下面的行将生成一个错误:
ifSome(Option("hi") -> f.setB _)
<console>:11: error: ambiguous reference to overloaded definition,
both method setB in class Foo of type (b: Int)Unit
and method setB in class Foo of type (b: String)Unit
match expected type ?
ifSome(Option("hi") -> f.setB _)
但是编译器知道我们要找的是一个Function1java.lang.String,_,那么为什么Function1Int的存在会引起混淆呢?我是不是遗漏了什么,或者这是一个编译器错误(或者它可能应该是一个特性请求)?
我可以通过使用如下所示的类型注释来解决此问题
ifSome(Option("hi") -> (f.setB _:String=>Unit))
但我想知道为什么这是必要的。
发布于 2012-09-29 13:13:24
你会想要尝试$ scalac -Ydebug -Yinfer-debug x.scala
,但首先你会想要最小化。
在本例中,您将看到在curried版本中,B是如何在第一个参数列表中求解的:
[infer method] solving for B in (bs: B*)(bfs: Function1[B, _]*)Nothing
based on (String)(bfs: Function1[B, _]*)Nothing (solved: B=String)
对于未加糖的版本,你会看到一些奇怪的地方
[infer view] <empty> with pt=String => Int
因为它试图消除重载的歧义,这可能会导致下面奇怪的解决方案。
虚拟隐式的唯一目的是解决过载,以便推理可以继续进行。隐式本身未使用,并且保持未实现的???
这是一个非常奇怪的解决方案,但是你知道重载是邪恶的,对吧?你必须使用任何你可以使用的工具来对抗邪恶。
另请注意,您的类型注释变通方法比仅以正常方式指定类型参数更费力。
object Test extends App {
def f[B](pairs: (B, B => _)*) = ???
def f2[B](bs: B*)(bfs: (B => _)*) = ???
def g(b: String) = ???
def g(b: Int) = ???
// explicitly
f[String](Pair("hi", g _))
// solves for B in first ps
f2("hi")(g _)
// using Pair instead of arrow means less debug output
//f(Pair("hi", g _))
locally {
// unused, but selects g(String) and solves B=String
import language.implicitConversions
implicit def cnv1(v: String): Int = ???
f(Pair("hi", g _))
}
// a more heavy-handed way to fix the type
class P[A](a: A, fnc: A => _)
class PS(a: String, fnc: String => _) extends P[String](a, fnc)
def p[A](ps: P[A]*) = ???
p(new PS("hi", g _))
}
发布于 2012-09-29 06:34:32
Scala中的类型推断只适用于从一个参数列表到下一个参数列表。因为您的ifSome
只有一个参数列表,所以Scala不会推断任何东西。您可以按如下方式更改ifSome
:
def ifSome[B, _](opts:Option[B]*)(funs: (B => _)*) {
val pairs = opts.zip(funs)
for((paramOption, setFunc) <- pairs)
for(someParam <- paramOption) setFunc(someParam)
}
让Foo保持原样...
class Foo{
var b=""
def setB(b:String){this.b = b}
def setB(b:Int){this.b = b.toString}
}
val f = new Foo
并相应地更改对ifSome
的调用:
ifSome(Option("hi"))(f.setB _)
这一切都很好用。当然,现在您必须检查opts
和funs
在运行时是否具有相同的长度。
https://stackoverflow.com/questions/12648019
复制相似问题