Skip to content

Confusing interaction between opaque types and extension methods #9880

Closed
@mpilquist

Description

@mpilquist

Minimized code

opaque type Bytes = Array[Byte]
object Bytes:
  def fromArray(arr: Array[Byte]): Bytes = arr
  extension (self: Bytes):
    def toArray: Array[Byte] = self
    def length: Int = (self: Array[Byte]).length
    def size: Int = (self: Array[Byte]).size

val b = Bytes.fromArray(Array(1, 2, 3))

object Main extends App {
  println("Length: " + b.length)
  println("Size: " + b.size)
}

Output

Length is printed and then an infinite loop is encountered on b.size

Expectation

Array[Byte] doesn't have a size member so (self: Array[Byte]) is lifted to Bytes instead of picking up ArrayOps implicit conversion. This seems like a pretty big gotcha but not sure how to improve.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions