You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
objectrepro {
importscala.language.implicitConversionscaseclassCommutativePair[A, B](a: A, b: B)
objectCommutativePair {
// Support a kind of commutative behavior via an implicit reorderingimplicitdefreorderPair[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.deff(p: CommutativePair[String, Int]) = p.toString
valt=CommutativePair(3, "c")
// This works: the implicit reordering is appliedvalr1= f(t)
// This fails to compile: the implicit reordering is ignored by the compilervalr2= f(CommutativePair(3, "c"))
// Explicitly annotating type compiles and works correctlyvalr3= f(CommutativePair[Int, String](3, "c"))
}
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: