作者yauhh (哟)
看板Programming
标题Re: [问题] Scala上generic和implicit的怪问题
时间Tue Jan 15 04:12:30 2013
三段程式没有很像啊
※ 引述《coolcomm (coolcomm)》之铭言:
: // (3) 1 + 2
: abstract class Abstract3 {
: type TP
: protected def action(arg: TP): TP
: def *[T <% TP](arg: T) = action(arg) //这里
: }
这里有一个 method `action` of type (arg: TP): TP
然後 * 不确定是否能够定义为函数名称
: class Concrete3(str: String) extends Abstract3 {
: type TP = Concrete3
: override protected def action[T <% TP](arg: T) = new TP("")
: }
但是这里跑一个 method `action` 为把 type T 换成 type TP
: class Test3 {
: implicit def str2Concrete(s: String)(implicit num: Int) = new Concrete3(s)
: implicit val a = 1 //^^^^^^^^^^^^^^^^^^^和这里
: val foo = "AA" * "BB" * "CC" //错误
: }
: 结果第一段和第二段程式码编译成功 而第三段却编译失败
: 请问这是为什麽...?
改写成以下这样,似乎OK:
abstract class Abstract3 {
type TP
protected def action(arg: TP): TP
def x[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" x "BB" x "CC"
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 36.226.94.177
1F:推 coolcomm:囧...我发现我还没完全了解view bound 36.224.127.140 01/15 11:21
2F:→ coolcomm:问题还真的是那个* 改成/或&都没问题XD 36.224.127.140 01/15 11:23
3F:→ yauhh:我连val v是什麽东西,理解都有待加强 36.226.94.177 01/15 11:52