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
This session from my REPL is probably more explanatory than I can be:
scala>caseclassClass1()
defined classClass1
scala>caseclassClass2(implicitclass1: Class1) {
|defprintClass1() = println(class1)
| }
defined classClass2
scala>objectTest {
|valclass2=newClass2|implicitvalclass1=newClass1()
| }
<console>:12:error: could not find implicit value for parameter class1: Class1valclass2=newClass2
^
scala>objectTest {
|valclass2=newClass2|implicitvalclass1:Class1=newClass1()
| }
defined module Test
scala>Test.class2.printClass1()
null
scala>objectTest {
|implicitvalclass1=newClass1()
|valclass2=newClass2| }
defined module Test
scala>Test.class2.printClass1()
Class1()
This is an example of using implicit parameters as a basic inversion of control in the first definition of the module Test the compiler is correctly failing as there is no definition at that point. However, in the second definition of Test, the compiler appears to allow this through, but passes the reference to class1 before it is initialized (confirmed using -Xprint:typer). The final definition is the fully valid case.
It would appear that specifying the type of class1 somehow causes that code to be valid.
The text was updated successfully, but these errors were encountered:
@paulp said:
I wanted to say "as specified", but I can't find it in the spec. If it's not in there it's way overdue to be in there. Anyway, this is "as intended." See comments in #801.
Sean Parsons (seanparsons) said:
The comments in that bug are for implicit conversions aren't they? In either case I'd say this is pretty confusing in its current state.
Here is a simplified example that I have tried in Scala 2.13.x.
caseclassA()
objectTest {
deff(implicita: A) =42implicitvala/* :A */=A()
}
Like the original example, if the type annotation of a is commented, then the compiler rejects this program. If the type annotation is uncommented, then the compiler accepts the program but warns that a is uninitialised. This shows two issues:
In the first case, the implicit search for resolving f does not account for the implicit declaration of a, because the type is not annotated and it has not been inferred yet. If the specification states that the parameter to f should be resolved before inferring the type of a, then we are getting the expected behaviour.
In the second case, in which it is using a non-initialised value, that is the same problem as if we were not using implicit values.
All in all, I am not sure if this should be regarded as expected behaviour, and is therefore not a bug.
This session from my REPL is probably more explanatory than I can be:
This is an example of using implicit parameters as a basic inversion of control in the first definition of the module Test the compiler is correctly failing as there is no definition at that point. However, in the second definition of Test, the compiler appears to allow this through, but passes the reference to class1 before it is initialized (confirmed using -Xprint:typer). The final definition is the fully valid case.
It would appear that specifying the type of class1 somehow causes that code to be valid.
The text was updated successfully, but these errors were encountered: