Basic extractor syntax

#1

Hi,

I’m getting the basics wrong for an extractor.

I would like to have a wrapper class, for example

class A extends Iterable[Int] {
val list = List(1,2,3)
def iterator = list.iterator
}

I would like then to pattern match over objects of type A.

I have the following:

object A {
def unapply(a: A): Option[List[Int]] = Some(a.list)
}

However, if I create an object of type A and pattern match over it, I get:

scala> new A() match {
| case List() => true
| }
:15: warning: fruitless type test: a value of type A cannot also be a List[A]
case List() => true
^

I’m sure its a simple mistake. Any advice welcome.

#2

What you probably want is:

new A() match {
  case A(List()) => true
  case _ => false
  } 
#4

Also see this article for more examples on how to write and use extractors.