Black monkey house: Scala implicit class

After scala2.10 provide implicit classes implicit declaration class may be used, but note the following points:
(1) configuration parameters and which are carried by only one
(2) must be defined implicitly in the class " class "or" companion object "or" package object ", the top can not
do with the definition of where they are actually defined in the object inside, that must have been instantiated in scope, the implicit definition of class
(3) implicit class can not case class (defined case class will automatically generate an object associated with the conflict 2)
(4) does not have scope identifier with the same name

object StringUtils {
  //隐士类
  implicit class StringImprovement(val s:String){
    def increment = s.map(x => (x + 1) toChar)
  }
}

object StringMain{
  def main(args: Array[String]): Unit = {
    import unit15.StringUtils._
    println("abcde".increment)
  }
}

Reproduced in: https: //www.jianshu.com/p/f8104f838433

Guess you like

Origin blog.csdn.net/weixin_34211761/article/details/91182539