Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Scala apply multiple implicit conversions in one expression? [duplicate]

Tags:

scala

Possible Duplicate:
How can I chain implicits in Scala?

Can Scala apply multiple implicit conversions in one expression?

Consider this simple example:

  case class Wrapper(s: String)

  case class WrapperWrapper(w: Wrapper)

  implicit def string2Wrapper(s: String) = Wrapper(s)

  implicit def wrapper2WrapperWrapper(w: Wrapper) = WrapperWrapper(w)

  // implicit conversation -->  w = string2Wrapper("w")
  val w: Wrapper = "w"

  // implicit conversation -->  ww = wrapper2WrapperWrapper(w)
  val ww: WrapperWrapper = w

  // does NOT compile!
  // ideally --> sad = wrapper2WrapperWrapper(string2Wrapper("ww"))
  val sad: WrapperWrapper = "ww"

Is there any way to get the "double" conversion in the last line to work?

I can help things along by defining another implicit like:

implicit def string2WrapperWrapper(s:String) = wrapper2WrapperWrapper(s)

but it seems like that shouldn't be necessary...

like image 937
Brian Kent Avatar asked Dec 21 '22 06:12

Brian Kent


1 Answers

I'm afraid I don't have a concise reference to hand (the answer is scattered through 6.26 and chapter 7 of The Scala Language Specification), but the answer is no.

This is due to practicality - double implicit conversions would square the number of possible conversions available, greatly increasing the chance of collisions and making it harder to work out exactly what would happen with a given conversion.

It's not ideal that you have to declare the String to WrapperWrapper conversion yourself; but given how rarely you need to do this in practice, compared to the potential confusion of double implicits, I do consider it the lesser of two evils.

like image 125
Andrzej Doyle Avatar answered Jan 17 '23 17:01

Andrzej Doyle