1 year ago

#383892

test-img

Maximilien Tirard

Why can two type aliases not have extension methods with the same name?

In a Scala 3 REPL, entering the following

type B = Int

extension (a: A)
  def f() = println("A!")

extension (b: B)
  def f() = println("B!")

gives the error

8 |  def f() = println("B")
  |      ^
  |Double definition:
  |def f(a: A)(): Unit in object rs$line$1 at line 5 and
  |def f(b: B)(): Unit in object rs$line$1 at line 8
  |have the same type after erasure.
  |
  |Consider adding a @targetName annotation to one of the conflicting definitions
  |for disambiguation.

Why can't I do this?

I was hoping that in the following, the type definitions would be sufficient for the compiler / for Scala to know which implementation to use

val a: A = 0
val b: B = 0
val x: Int = 0

a.f() // I would expect "A!"
b.f() // I would expect "B!"
x.f() // I would expect an error

I suspect opaque type aliases are what I'm looking for?

scala-3

0 Answers

Your Answer

Accepted video resources