Scala 方法名为"*&引用;导致编译错误
我对这段代码有点困惑:Scala 方法名为"*&引用;导致编译错误,scala,operator-keyword,operator-precedence,Scala,Operator Keyword,Operator Precedence,我对这段代码有点困惑: abstract class Abstract3 { type TP protected def action(arg: TP): TP def *[T <% TP](arg: T) = action(arg) } class Concrete3(str: String) extends Abstract3 { type TP = Concrete3 override protected def act
abstract class Abstract3 {
type TP
protected def action(arg: TP): TP
def *[T <% TP](arg: T) = action(arg)
}
class Concrete3(str: String) extends Abstract3 {
type TP = Concrete3
override protected def action(arg: TP) = new TP("")
}
class Test3 {
implicit def str2Concrete(s: String)(implicit num: Int) = new Concrete3(s)
implicit val a = 1
val foo = "AA" * "BB" * "CC"
}
抽象类抽象3{
TP型
受保护的def操作(参数:TP):TP
def*[T我倾向于认为问题在于*
被Predef
重载为字符串(如“AA”*5
),因此,当您向它添加自己的*
时,转换过程中会出现歧义,这使得Scala放弃了这两个字符串
转换被丢弃后,剩下的就是在String
上查找*
,而它不在那里
这个想法的问题是,<代码>“AA”* 2 即使在导入的情况下仍然有效,而且您所添加的转换没有隐式参数。但是我仍然认为答案是这样的。
如果Daniel C. Sobrals的评论是正确的,您可以考虑通过命名操作符“:*”来解决问题。.看起来不太好,但做得很好。-Yinfer debug似乎说隐式int参数破坏了从视图t获得的转换
abstract class Abstract3 {
type TP
protected def action(arg: TP): TP
def /[T <% TP](arg: T) = action(arg)
}
class Concrete3(str: String) extends Abstract3 {
type TP = Concrete3
override protected def action(arg: TP) = new TP("")
}
class Test3 {
implicit def str2Concrete(s: String)(implicit num: Int) = new Concrete3(s)
implicit val a = 1
val foo = "AA" / "BB" / "CC"
}
abstract class Abstract3 {
type TP
protected def action(arg: TP): TP
def *[T <% TP](arg: T) = action(arg)
}
class Concrete3(str: String) extends Abstract3 {
type TP = Concrete3
override protected def action(arg: TP) = new TP("")
}
class Test3 {
implicit def str2Concrete(s: String) = new Concrete3(s)
val foo = "AA" * "BB" * "CC"
}