Last active
October 10, 2017 01:36
-
-
Save steinybot/121d24730edda4d815918c08b7d64a46 to your computer and use it in GitHub Desktop.
Surprising implicit resolutions with package objects
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
package com | |
package object example { | |
implicit class FooImplicit(val foo: Any) { | |
def fooImplicit = ??? | |
} | |
} |
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
package com.example | |
class Test { | |
// This works | |
(_: Int).fooImplicit | |
} |
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
package com.example.subpackage | |
class Test2 { | |
// This does not work | |
(_: Int).fooImplicit | |
// This works | |
this.fooImplicit | |
} |
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
package com.example2 | |
class Test3 { | |
// This does not work | |
(_: Int).fooImplicit | |
// This does work | |
(_: com.example.Test).fooImplicit | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The key takeaway here is that it is the scope of the type that is searched and not the scope of where it is used, except for the package object of the same package (not parent packages).