Skip to content

Opaque types and implicit specificity #9928

Closed
@vincenzobaz

Description

@vincenzobaz

Minimized code

trait Magic[F]:
  extension (x: Int) def read: F

object Magic:
  given Magic[String]:
    extension(x: Int) def read: String = 
      println("In string")      
      s"$x"

opaque type Foo = String
object Foo:
  import Magic.{given _}
  def apply(s: String): Foo = s

  given Magic[Foo]:
    extension (x: Int) def read: Foo =
      println("In foo")
      Foo(s"$x")
  
  def test: Unit =
    (3.read: Foo)
  

@main def test = {
  Foo.test
}

Output

In string

Expectation

In foo

Remarks

  • If import Magic.{given _} is removed, the output is as expected. The question is: should this import change the behavior? given Magic[Foo] should be more specific and preferred to given Magic[String]

Snippet

https://scastie.scala-lang.org/vincenzobaz/tGQhSpQ8TQyiOeTNAvSGbA/104

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions