Skip to content

Opaque type extension method puzzler #7821

Closed
@nicolasstucki

Description

@nicolasstucki

minimized code

X and MyX are both opaque typed defining extension methods in the same way.

object XObject {
  opaque type X = Int

  def anX: X = 5

  given ops: Object {
    def (x: X) + (y: X): X = x + y
  }
}

object MyXObject {
  opaque type MyX = XObject.X

  def anX: MyX = XObject.anX

  given ops: Object {
    def (x: MyX) + (y: MyX): MyX = x + y
  }
}

object Main extends App {
  println(XObject.anX + XObject.anX) // prints 10
  println(MyXObject.anX + MyXObject.anX) // infinite loop
}

The issue is that when we look + in def (x: MyX) + (y: MyX): MyX = x + y we know that x: XObject.X and therfore both XObject.ops.+ and MyXObject.ops.+ are valid extensions for + but MyXObject.ops.+ is chosen becuase it is closer in scope. The hope here wast to use the primitive XObject.ops.+ on x.

Defining the extensions outside MyXObject fixes the issue but then the extension are not automatically available for the opaque type MyXObject.X.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions