Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Extension methods do not have priority - is this intended? #20242

Unanswered
scf37 asked this question in General Question
Discussion options

When defining extension methods via good old implicits, it is possible to specify priority:

case class AOpsLow(a: A) {
 def foo: String = "low"
}
case class AOpsHigh(a: A) {
 def foo: String = "high"
}
trait LowPriorityImplicits {
 implicit def lowPriorityOps(a: A): AOpsLow = AOpsLow(a)
}
class A
object A extends LowPriorityImplicits {
 implicit def highPriorityOps(a: A): AOpsHigh = AOpsHigh(a)
}
println(A().foo) // prints "high"

While it does not work using new extension methods syntax:

class LowPriorityOps {
 extension(a: A)
 def foo: String = "low"
}
trait LowPriorityImplicits {
 given LowPriorityOps = LowPriorityOps()
}
class A
object A extends LowPriorityImplicits {
 extension(a: A)
 def foo: String = "high"
}
println(A().foo) // Note that implicit extension methods cannot be applied because they are ambiguous;
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /