Skip to content

Non-specific message: "not a valid result type of an unapply method of an extractor" #17077

Closed
@dsbos

Description

@dsbos

(Some) errors with extractor unapply methods yield only a general "not a valid result type of an unapply method of an extractor" message, which doesn't really help the user identify and fix whatever the actual error is.

Compiler version

3.2.2

Minimized example

object ExtractorsMessages {

  {  // Works fine:
    case class TypicalCaseClass(x: String)

    object Extractor:
      def unapply(x: String): TypicalCaseClass = ???

    (null: AnyRef) match
      case Extractor(out1) =>
  }

  {
    case class NoArgCaseClass()

    object Extractor:
      def unapply(x: String): NoArgCaseClass = ???

    (null: AnyRef) match
      // *** On following line, compiler reports just "NoArgCaseClass is not a
      // valid result type of an unapply method of an extractor" (neither
      // identifying the specific condition(s) that failed to be met (in this
      // case, having at least one product element (right?)) nor stating the
      // conditions that need to be met (even it can't/doesn't identify which
      // part failed to be met)):
      case Extractor() =>
  }

  { // Attempt at creating non-case class with "manual" additions to make it
    // sufficient to use with an extractor:

    class AttemptedPlainClass(val x: String) extends Product
    object AttemptedPlainClass {
      def apply(x: String): AttemptedPlainClass = ???
    }

    object Extractor:
      def unapply(x: String): AttemptedPlainClass = ???

    (null: AnyRef) match
      // *** Again, compiler just says "... is not a valid result type of an
      // unapply method of an extractor."  (In this case, I don't yet know why
      // it's not valid (presumbly something case classes have that I forgot to
      // provide for my non-case class), since the compiler didn't say anything
      // more and since
      // https://docs.scala-lang.org/scala3/reference/changed-features/pattern-matching.html#product-match
      // seems quite unclear):

      case Extractor(out1) =>
  }

}

Output

At case Extractor(out1) => lines annotated with comments above, compiler errors say:
NoArgCaseClass is not a valid result type of an unapply method of an extractor.
and
AttemptedPlainClass is not a valid result type of an unapply method of an extractor.

Expectation

(If not a valid expectation, then at least a hope/wish/eventual improvement:) That the compiler would be more specific about why the results types are not valid.

(FYI, I was exploring Scala 3 extractors, trying to follow the documentation at https://docs.scala-lang.org/scala3/reference/changed-features/pattern-matching.html, which seems a little unclear.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions