Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit conversion is properly invoked in some cases but not others #9447

Open
scabug opened this issue Aug 25, 2015 · 2 comments
Open

Implicit conversion is properly invoked in some cases but not others #9447

scabug opened this issue Aug 25, 2015 · 2 comments

Comments

@scabug
Copy link

scabug commented Aug 25, 2015

Below is a simple reproducer, where I define a "commutative" pair type that comes with an implicit reordering conversion.

The implicit conversion is applied by the compiler as expected if the argument to function f is in a pre-existing named value (t in the example). However, if I try to call f directly on literal CommutativePair, it fails with a type error. The compiler is not applying the implicit reordering conversion in that case. It appears to be related to type inference. Explicitly annotating the type will also work properly.

See also:
http://stackoverflow.com/questions/32211381/scala-implicit-conversion-is-applying-under-some-conditions-but-not-others

object repro {
  import scala.language.implicitConversions

  case class CommutativePair[A, B](a: A, b: B)

  object CommutativePair {
    // Support a kind of commutative behavior via an implicit reordering
    implicit def reorderPair[B, A](pair: CommutativePair[B, A]) =
      CommutativePair(pair.b, pair.a)
  }

  // The idea is to allow a call to 'f' with Pair[Int, String] as well,
  // via implicit reorder.
  def f(p: CommutativePair[String, Int]) = p.toString

  val t = CommutativePair(3, "c")

  // This works: the implicit reordering is applied
  val r1 = f(t)

  // This fails to compile: the implicit reordering is ignored by the compiler
  val r2 = f(CommutativePair(3, "c"))

  // Explicitly annotating type compiles and works correctly
  val r3 = f(CommutativePair[Int, String](3, "c"))
}
@scabug
Copy link
Author

scabug commented Aug 25, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9447?orig=1
Reporter: @erikerlandson
Affected Versions: 2.10.3, 2.11.7

@scabug
Copy link
Author

scabug commented Apr 24, 2016

@Atry said:
This bug may be a regression Atry/zero-log#3 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant