Skip to content

Instantly share code, notes, and snippets.

@steinybot
Last active October 10, 2017 01:36
Show Gist options
  • Save steinybot/121d24730edda4d815918c08b7d64a46 to your computer and use it in GitHub Desktop.
Save steinybot/121d24730edda4d815918c08b7d64a46 to your computer and use it in GitHub Desktop.
Surprising implicit resolutions with package objects
package com
package object example {
implicit class FooImplicit(val foo: Any) {
def fooImplicit = ???
}
}
package com.example
class Test {
// This works
(_: Int).fooImplicit
}
package com.example.subpackage
class Test2 {
// This does not work
(_: Int).fooImplicit
// This works
this.fooImplicit
}
package com.example2
class Test3 {
// This does not work
(_: Int).fooImplicit
// This does work
(_: com.example.Test).fooImplicit
}
@steinybot
Copy link
Author

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).

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