Search code examples
scalaimplicit-conversionimplicit

Two implicit definitions with same name for a method


I have two implicit declarations that "redefine" x as an operator:

import scala.io.StdIn._
import util._
import scala.language.postfixOps


case class Rectangle(width: Int, height: Int)
case class Circle(ratio: Integer)
case class Cylinder[T](ratio: T, height: T)


object implicitsExample1 {

    implicit class RectangleMaker(width: Int) {
        def x(height: Int) = Rectangle(width, height)
    }

    implicit class CircleMaker(ratio: Int) {
        def c = Circle(ratio)
    }

    implicit class CylinderMaker[T](ratio: T) {
        def x(height: T) = Cylinder(ratio, height)
    }


    def main(args: Array[String]) {

        val myRectangle = 3 x 4
        val myCircle = 3 c
        val myCylinder = 4 x 5

        println("myRectangle = " + myRectangle)

        println("myCircle = " + myCircle) 

        println("myCylinder = " + myCylinder)

    }

}

Here my output gives:

myRectangle = Rectangle(3,4)
myCircle = Circle(3)
myCylinder = Rectangle(4,5)

What I need to do to have something like:

myCylinder = Cylinder[Int](4,5)

I understand that the chosen implicit conversion is the first one declared but is there a way to specify the use of the Cylinder one?


Solution

  • Try combining RectangleMaker and CylinderMaker into a single ShapeMaker implicit class like so

    implicit class ShapeMaker[T](width: T) {
      def x(height: T)(implicit ev: T =:= Int) = Rectangle(width, height)
      def x(height: T) = Cylinder[T](width, height)
    }
    

    and provide type ascriptions to value definitions like so

    val myRectangle: Rectangle = 3 x 4
    val myCircle = 3 c
    val myCylinder: Cylinder[Int] = 4 x 5
    

    which outputs

    myRectangle = Rectangle(3,4)
    myCircle = Circle(3)
    myCylinder = Cylinder(4,5)