Skip to content

introduce givenInstanceNotFound to retire the implicitNotFound? #17765

@He-Pin

Description

@He-Pin

Compiler version

Scala 3.1.x

Minimized code

import scala.annotation.implicitNotFound

@implicitNotFound("Could not find an implicit C[${T}, ${U}]")
class C[T, U]

class K[A] {
  def m[B](implicit c: C[List[A], B]) = 0
  def n[B](implicit @implicitNotFound("Specific message for C of list of ${A} and ${B}") c: C[List[A], B]) = 1
}

object Test {
  val k = new K[Int]
  k.m[String]
  k.n[String]
}

Output

-- Error: ----------------------------------------------------------------------
3 |  k.m[String]
  |             ^
  |             Could not find an implicit C[List[Int], String]
-- Error: ----------------------------------------------------------------------
4 |  k.n[String]
  |             ^
  |             Specific message for C of list of Int and String
2 errors found

image

Expectation

  @givenIntanceNotFound("Could not find an implicit C[${T}, ${U}]")
  class C[T, U]

  class K[A] {
    def m[B](using c: C[List[A], B]) = 0
  }

  object Test {
    val k = new K[Int]
    k.m[String]
  }

given:

-- Error: ----------------------------------------------------------------------
3 |  k.m[String]
  |             ^
  |             Could not find a given instance of C[List[Int], String] in scope

If I remove the @implicitNotFound line , it gives:

[error] -- Error: C:\Users\hepin\IdeaProjects\leetCode\src\main\scala\test\NotFoundImplicit.scala:21:15
[error] 21 |    k.m[String]
[error]    |               ^
[error]    |no given instance of type test.NotFoundImplicit.C[List[Int], String] was found for parameter c of method m in class K

So I think we would introduce a new annotation scala.annotation.givenIntanceNotFound?

Metadata

Metadata

Assignees

No one assigned

    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