Scala 模糊重载:修复它还是尝试其他方法?

Scala 模糊重载:修复它还是尝试其他方法?,scala,implicit,scala.js,scala.rx,scalatags,Scala,Implicit,Scala.js,Scala.rx,Scalatags,背景:我正在使用/与合作。我试图实现的是使用操作符样式将html输入的值绑定到RxVars。以下是我的计划: implicit class BoundHtmlInput(input: Input) { def bindTextTo(v: Var[String]): Input = { input.oninput = { (e: dom.Event) => v() = input.value} input } def bindNumberTo(v: Var[In

背景:我正在使用/与合作。我试图实现的是使用操作符样式将html输入的值绑定到Rx
Var
s。以下是我的计划:

implicit class BoundHtmlInput(input: Input) {
  def bindTextTo(v: Var[String]): Input = {
    input.oninput = { (e: dom.Event) => v() = input.value}
    input
  }

  def bindNumberTo(v: Var[Int]): Input = {
    input.oninput = {(e: dom.Event) => v() = input.valueAsNumber}
    input
  }

  def ~>[T](v: Var[T])(implicit ev: T =:= Int): Input =
     bindNumberTo(v.asInstanceOf[Var[Int]])

  def ~>[T](v: Var[T])(implicit ev: T =:= String): Input = 
     bindTextTo(v.asInstanceOf[Var[String]])
}
对于方法调用,它可以正常工作,但对于运算符
~>
调用,它失败。错误如下:

Error:(43, 70) ambiguous reference to overloaded definition,
both method ~> in class BoundHtmlInput of type [T](v: rx.Var[T])(implicit ev: =:=[T,String])org.scalajs.dom.html.Input
and  method ~> in class BoundHtmlInput of type [T](v: rx.Var[T])(implicit ev: =:=[T,Int])org.scalajs.dom.html.Input
match argument types (rx.core.Var[String])
我也不喜欢用
来代替


我希望这能提供足够的背景。我的问题是,实现我想要的更好的方法是什么?

直接使用
Var[Int]
Var[String]
,使用虚拟隐式在擦除后消除双def:

def ~>[T](v: Var[Int]): Input =
  bindNumberTo(v)

def ~>[T](v: Var[String])(implicit dummy: DummyImplicit): Input = 
  bindTextTo(v)
如果有更多类型需要处理,则可以根据需要添加任意数量的
DummyImplicit
s:

def ~>[T](v: Var[Boolean])(implicit dummy1: DummyImplicit,
    dummy2: DummyImplicit): Input = 
  bindBooleanTo(v)

在普通重载上使用广义类型约束有什么原因吗?那么我得到:
double definition:def>(v:rx.Var[Int]):org.scalajs.dom.html.第26行的输入和第27行的输入具有相同的类型:(v:rx.core.Var)
可能只制作一个
~>
并使用
类型标签,如下所示:有一个简单的解决方法:添加一个伪隐式(只是为了使签名不同):
def~>(v:Var[Int]):Input
def~>(v:Var[String])(隐式d:DummyImplicit):Input
。如果您有更多的重载需要定义,那么其他替代方法之一就是使用磁铁模式(googleit)。(编辑:尽管您仍然可以像sjrd刚刚建议的那样使用几个
DummyImplicit
参数)@ukasz
TypeTags
使用反射,这在
scala.js