-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Type inference on class member widened to abstract definition #17324
Comments
This is expected, because the override is inferred to have the type of the member that is overridden. This is true for scala 2 under The |
The change from scala2 to scala3 has been taken, to prevent unwanted exposure of implementation detail (of specific type information in the subclass). Correct ? Sometimes, the sub-class is not simply an implementation but a specialization of its super-class. I already mentioned The code below is the illustration of the feature in scala-doc: trait Vehicle extends reflect.Selectable:
val wheels: Int
val i3 = new Vehicle: // i3: Vehicle { val range: Int }
val wheels = 4
val range = 240
i3.range The feature is designed to craft the correct refined type trait Vehicle extends reflect.Selectable:
val wheels: Int
trait Fleet:
val vehicle : Vehicle
class MyFleet extends Fleet :
val vehicle = new Vehicle: // vehicle : Vehicle ** UNREFINED **
val wheels = 4
val range = 240
val f = new MyFleet
f.vehicle.range // Does not compile The precious type information is lost ! Today, I may use |
Some pointers : (Thank you @smarter !) why a class member should be widened to its super interface type:
A SIP, for a Why are Closing. |
I would expect |
Compiler version
3.2.2 , 3.3.0-RC3
Minimized code
Output
Expectation
The issue is the type of the
val objN
inY
.obj0
: EXPECTED. Type isB
as inferred fromchoose( )
return type.obj1
: UNEXPECTED. Type isA
, widened to the abstract definitionval obj1:A
inX
obj2
: EXPECTED. Type isB
, from the type-ascription.obj3
: ???. Type isB
. Thefinal
modifier has an impact on the member type (vs.obj1
), and seems to restore expected type inference. On the other hand making the entireclass Y
final has no impact.Observations
You may notice a pattern very close to the one described here. The bug is not caused bug
transparent inline
, but loses the on-purpose crafted type.In scala2 the type was the expected one. scastie
final
on the type of the member expected ?The text was updated successfully, but these errors were encountered: