-
Notifications
You must be signed in to change notification settings - Fork 1.1k
An implicit val should not be available for defining itself? #6114
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
Comments
Paging @milessabin since this reminds me of |
Forward definitions, also are available to the implicit scope: scala> def test: Unit = { implicitly[Int]; implicit val foo: Int = 1 }
<console>:11: error: forward reference extends over definition of value foo
def test: Unit = { implicitly[Int]; implicit val foo: Int = 1 } Some discussions about what should be and not be in the implicit scope happened here. If I remember correctly, at the time @odersky said it was tricky to exclude some vals from the implicit scope and better to have the forward reference checker reports invalid implicitly inferred references. |
You're seeing the forward reference error in this case because Self references of this sort are unproblematic and should be accepted if the implicit argument they correspond to is by-name, ie. |
It has to do with initialization, if |
Yes, but that's orthogonal to the implicitness or otherwise of the definitions and arguments here. |
@odersky suggested that a migration for the following code: def libMethod(implicit x: Int): Int = ???
implicit val m: Int = 10
def bar: Unit = {
implicit val x = libMethod
implicitly[Int]
} would be like the follows: def libMethod(implicit x: Int): Int = ???
implicit val m: Int = 10
def bar: Unit = {
implicit def x(implicit x: Int): Int = libMethod
implicitly[Int]
} I verified that it indeed works in Dotty. However, the code above doesn't compile in Scala 2.12. |
Why does it not compile? Is it that you get a divergence error? |
Scala2 produces the following error:
/cc: @adriaanm @milessabin |
Looking back at the initial example it seams like it's exploiting a loop hole in scalac which which requires an implicit without explicit return type. Is there any action to be taken here or should we just close the issue? |
Close, the Dotty behavior is principled. |
Uh oh!
There was an error while loading. Please reload this page.
The following code compiles in Scalac 2.12. However, if we give explicit type to
x
, it compiles neither in Scalac nor Dotty.Error message:
Such code exists in ScalaTest:
The text was updated successfully, but these errors were encountered: