0

Any idea how to get the following to work:

trait HttpTransport {
  def doGet(str: String): String
}

trait CoreGet {
  def GET(str: String)(implicit imp:String): List[String]
}


trait VerbGet extends CoreGet with HttpTransport {
  override def GET(str: String)(implicit imp:String): List[String]= {
    println("->VerbGet.GET")
    val str1 = doGet(str)
    // and more biz logic calls here
    List(s"\nverb (biz logic) called url $str and got '${str1}'>")
  }
}

// PlayGet {
implicit class ExtendCoreGet(coreGet: CoreGet) {


  def GET[A](url: String)(implicit imp:String, imp2: List[A]): List[A]= {
    println(s"->ExtendCoreGet.GET($url)")

    val coreGetResult = coreGet.GET(url)
    coreGetResult.flatMap(_ => imp2)


  }
}

trait Play extends HttpTransport {
  override def doGet(str: String): String = {
    println("->Play.doGet")
    s"\nPlay.doGet($str)>"

  }
}



val client = new VerbGet with Play

client.GET("www.go.com")("hello", List("1"))  //<-- does not compile

Compiler error:

too many arguments (2) for method GET: (implicit imp: String)List[String]

You can play with the code here: https://scastie.scala-lang.org/arminio/tN9NfdxGQUmusrNL0lJ78w

Armin
  • 1,367
  • 1
  • 12
  • 17
  • 1
    A method can be overloaded by differentiating the 1st parameter group. It can't be done after that. – jwvh Jun 25 '17 at 14:42

1 Answers1

0

It looks like you are trying extend functionality of VerbGet. You need to fix two things:
1. ExtendCoreGet must extend AnyVal to add more methods.
2. You can add new functionality by adding new method say GET2 but you can't overload existing method. Renmaed your GET to GET2 or something meaningful.

ExtendCoreGet definition should be

implicit class ExtendCoreGet(val coreGet: CoreGet) extends AnyVal {
  def GET2[A](url: String)(implicit imp:String, imp2: List[A]): List[A]= {
    coreGet.GET(url).flatMap(_ => imp2)
  }
}
Chenna Reddy
  • 2,241
  • 18
  • 17
  • I am afraid the whole purpose of this exercise is to overload GET. Renaming it to GET2 is not going to work for me. So are you saying that it is not possible to overload GET at all? – Armin Jun 25 '17 at 13:50
  • It should be possible with normal functions (no currying), but with currying, not sure how compiler can decided when you say GET("hello"). – Chenna Reddy Jun 25 '17 at 14:48
  • 1. It usually improves performance (not by much), but "must" is wrong. – Alexey Romanov Jun 26 '17 at 07:00
  • So it seems that when curried functions are overloaded, resolution in Scala takes only the first parameter list into account: https://stackoverflow.com/a/7182739/317683 (see the answer by Martin Odersky) – Armin Jun 26 '17 at 07:04