Value classes introduce unwanted public methods

前端 未结 5 2295
孤街浪徒
孤街浪徒 2021-02-19 03:44

Looking at some scala-docs of my libraries, it appeared to me that there is some unwanted noise from value classes. For example:

implicit class RichInt(val i: In         


        
5条回答
  •  自闭症患者
    2021-02-19 04:26

    A possibility is to use a name that is shadowed:

    implicit class IntOps(val toInt: Int) extends AnyVal {
      def squared = toInt * toInt
    }
    

    Or

    implicit class IntOps(val toInt: Int) extends AnyVal { ops =>
      import ops.{toInt => value}
      def squared = value * value
    }
    

    This would still end up in the scala-docs, but at least calling 4.toInt is neither confusing, no actually triggering IntOps.

提交回复
热议问题