原代码如下:

class Person(val name: String, val age: Int) {
    override def toString: String = this.name + " " + this.age
}

object MyImplicit {
    implicit def personToOrdered(p: Person)  = new Ordered[Person] {
        override def compare(that: Person): Int = {
            p.age - that.age
        }
    }
}

隐式转换已引入,报错信息如上所述:

问题修改是将隐式转换函数定义时,显式定义返回类型:

implicit def personToOrdered(p: Person) : Ordered[Person] = new Ordered[Person] {
        override def compare(that: Person): Int = {
            p.age - that.age
        }
}

 

posted on 2022-04-09 16:12  啊哈咧  阅读(75)  评论(0编辑  收藏  举报