best practices for 'scala.MatchError' when utilizing custom extractor for pattern matching?
This might be a silly question, but I've encountered a strange issue with I've looked through the documentation and I'm still confused about I'm stuck on something that should probably be simple. I'm working with a `scala.MatchError` when trying to use a custom extractor in Scala. My extractor is defined like this: ```scala object UserExtractor { def unapply(user: User): Option[(String, Int)] = { if (user != null && user.name.nonEmpty) Some((user.name, user.age)) else None } } ``` I have a case class representing `User`: ```scala case class User(name: String, age: Int) ``` I'm trying to use the extractor in pattern matching like this: ```scala val users = List(User("Alice", 30), User("Bob", 25), User("", 40)) for (user <- users) { user match { case UserExtractor(name, age) => println(s"User: $name, Age: $age") case _ => println("No match") } } ``` I expected it to skip the empty name case, but instead, I'm getting the following behavior: ``` scala.MatchError: User(, 40) at UserExtractor.unapply(UserExtractor.scala:5) ``` I've confirmed that my extractor logic is correct and should handle the empty name case by returning `None`. However, it seems like the `match` statement is still attempting to evaluate the case for `UserExtractor` even when the name is empty. I've also verified that calling `UserExtractor.unapply` directly returns `None` for the empty name. Any suggestions on how to resolve this scenario or what I'm missing in the pattern matching process? I am using Scala 2.13.6. What's the best practice here? This is part of a larger application I'm building. Any help would be greatly appreciated! I'm working on a CLI tool that needs to handle this. What am I doing wrong? The project is a REST API built with Scala. Any ideas what could be causing this? What would be the recommended way to handle this?