-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Opaque type extension method puzzler #7821
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
This came from an issue encountered in #7795 which was minimized to object Foo {
opaque type MyArray = Array[Int]
def anArray: MyArray = Array(1, 2, 3)
given ops: Object {
def (arr: MyArray) drop(n: Int): MyArray = arr.drop(n)
}
}
object Main extends App {
println(Foo.anArray.drop(1))
} |
This can be worked around with def (x: MyX) + (y: MyX): MyX = XObject.ops.+(x)(y) |
@nicolasstucki What do you suggest should happen here? I agree it might be surprising, but as far as I can see, that's how it is supposed to work, no? |
Maybe this should have a warning. |
What would be the condition in which a warning should be issued? |
In general, if the RHS of the method only calls itself we can warn that this is an infinite recursive call. |
Fix #7821: Warn on simple infinite tailrec loops
minimized code
X
andMyX
are both opaque typed defining extension methods in the same way.The issue is that when we look
+
indef (x: MyX) + (y: MyX): MyX = x + y
we know thatx: XObject.X
and therfore bothXObject.ops.+
andMyXObject.ops.+
are valid extensions for+
butMyXObject.ops.+
is chosen becuase it is closer in scope. The hope here wast to use the primitiveXObject.ops.+
onx
.Defining the extensions outside
MyXObject
fixes the issue but then the extension are not automatically available for the opaque typeMyXObject.X
.The text was updated successfully, but these errors were encountered: