7

Suppose I have the code:

class A(implicit s:String = "foo"){println(s)}

object X {
   implicit val s1 = "hello"
}
object Y {
   import X._
   // do something with X
   implicit val s2 = "hi"
   val a = new A
}

I get the error:

<console>:14: error: ambiguous implicit values:
 both value s2 in object Y of type => String
 and value s1 in object X of type => String
 match expected type String
           val a = new A

Is there any way I can tell Scala to use the value s2 in Y? (if I rename s2 to s1, it works as expected but that is not what I want).

Another solution is to not do import X._, again something I'm trying to avoid.

Jus12
  • 17,824
  • 28
  • 99
  • 157

3 Answers3

11

Another thing you can do is to import everything but s1: import X.{s1 => _, _}.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
8

I agree with the other answer that explicitly providing the implicit in these types of situations is preferred, but if you insist on wanting to 'downgrade' the other implicit so it's no longer treated as an implicit then it is actually possible:

class A(implicit s:String = "foo"){println(s)}

object X {
  implicit val s1 = "hello"
}
object Y {
  import X._
  val s1 = X.s1 //downgrade to non-implicit

  // do something with X
  implicit val s2 = "hi"
  val a = new A
}

Again, this is a bit hackish, but it works.

cmbaxter
  • 35,283
  • 4
  • 86
  • 95
3

Try:

new A()(s2)

This should override the implicit parameter through er explicitly providing it.

Others
  • 2,876
  • 2
  • 30
  • 52
  • That defeats the purpose of using the implicit parameter. I have `new A` in several places and I'd rather just remove the implicit then. – Jus12 Feb 28 '15 at 09:36
  • @Jus12 I just don't see how you are going to be able to keep the wildcard import and not override the explicit at least once... – Others Feb 28 '15 at 09:37
  • Using an `implicit` for something small and objective like `strings` does not make sense in the first place. – sarveshseri Feb 28 '15 at 09:45
  • @SarveshKumarSingh I agree. The String is just for the sake of example. In my actual code its quite a complex object. – Jus12 Feb 28 '15 at 09:46
  • @Jus12 What happens if you name s2 the same as s1? Just out of curiosity – Others Feb 28 '15 at 09:48
  • 1
    And you have multiple instances of that complex object in scope. `Implicits` generally make sense for `Helper-functions`, `typeclass abstract evidences` such as `Monads` or large and meant to be reused large objects like `ActorSystem`. – sarveshseri Feb 28 '15 at 09:49
  • @Others: When I use `s1`, the behaviour is what I want and expected: It overrides the old one. However, I don't want the names to be the same due to other reasons. – Jus12 Feb 28 '15 at 15:06