I have an extractor which extracts an object from a string.
case class ItemStructure(id: String, data: String)
object ItemStructure {
def unapply(str: String): Option[ItemStructure] = {
str.split('-').toList match {
case List(id, data) => Some(ItemStructure(id, data))
case _ => None
}
}
}
If I try to use this extractor in a pattern matching, then all works as expected.
"" match {
case ItemStructure(item) =>
}
It also works in a pattern matching anonymous function.
Option("").map {
case ItemStructure(item) =>
}
Now, if I try to use this extractor in a partial function, the compiler fails with the message: cannot resolve overloaded unapply
val func: PartialFunction[Any, Unit] = {
case ItemStructure(item) =>
}
If I rename the companion object where the unapply function is located then all works as expected.
Could someone explain why the extract doesn't work if it's located in the companion object?