Created
December 17, 2009 23:11
-
-
Save retronym/259121 to your computer and use it in GitHub Desktop.
Investigation for https://lampsvn.epfl.ch/trac/scala/ticket/2811
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object test1 { | |
implicit def s2s(s: String): String = "s2s" | |
assert(implicitly[String => String].apply("foo") == "foo") | |
// Predef.conforms chosen here because it returns the required type (A => A), | |
// so no need to try to eta transform the method s2s. | |
} | |
object test2 { | |
locally { | |
// s2s is a def inside a block. it seems to be automatically eta transformed to (String => String), | |
// and is considered equally with conforms[A]. | |
implicit def s2s(s: String): String = "s2s" | |
// assert(implicitly[String => String].apply("foo") == "foo") | |
// UNCOMMENT LINE ABOVE FOR: | |
// error: ambiguous implicit values: | |
// both method s2s of type (s: String)java.lang.String | |
// and method conforms in object Predef of type [A]<:<[A,A] | |
// match expected type (String) => String | |
} | |
} | |
object test3 { | |
def s2s(s: String) = "s2s" | |
locally { | |
// here we ensure that (String => String) is implicit rather than a method. But why is this chosen now, rather than | |
// getting the ambiguity error fro test2? | |
implicit val _ = s2s _ | |
assert(implicitly[String => String].apply("foo") == "s2s") | |
} | |
locally { | |
implicit def conforms(s: String) = "s2s" // shadowing Predef.conforms by name works. But pretty it ain't. | |
assert(implicitly[String => String].apply("foo") == "s2s") | |
} | |
} | |
object test4 { | |
class X | |
class Y(val s: String) | |
object convert { | |
implicit def x2yFunctionObject: X => Y = (x: X) => new Y("x2yFunctionObject") | |
implicit def x2yDef(x: X): Y = new Y("x2yDef") | |
} | |
{ | |
import convert._ | |
assert(implicitly[X => Y].apply(new X).s == "x2yFunctionObject") | |
} | |
{ | |
import convert.x2yFunctionObject | |
assert(implicitly[X => Y].apply(new X).s == "x2yFunctionObject") | |
} | |
{ | |
import convert.x2yDef | |
assert(implicitly[X => Y].apply(new X).s == "x2yDef") | |
} | |
} | |
test1 | |
test2 | |
test3 | |
test4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment