Set not a case class and does not have an unapply method.
These two things mean that you cannot directly match patterns in Set .
( update : if you don't define your own extractor for Set , as Daniel correctly shows in his answer)
You should find an alternative, I would suggest using the bend function
def union(s: Set[Int], t: Set[Int]): Set[Int] = (s foldLeft t) {case (t: Set[Int], x: Int) => t + x}
or avoiding the most explicit type annotations
def union(s: Set[Int], t: Set[Int]): Set[Int] = (s foldLeft t)( (union, element) => union + element )
or even shorter
def union(s: Set[Int], t: Set[Int]): Set[Int] = (s foldLeft t)(_ + _)
This accumulates s elements by t , adding them one by one
folding
Here are the documents for the folding operation, if necessary for reference:
foldLeft[B](z: B)(op: (B, A) ⇒ B): B
Applies the binary operator to the initial value and all elements of this set from left to right.
Note: may return different results for different runs, unless the underlying collection type is ordered. or an associative and commutative operator.
B the result type of the binary operator. z the start value. op the binary operator. returns the result of inserting op between consecutive elements of this set, going left to right with the start value z on the left: op(...op(z, x_1), x_2, ..., x_n) where x1, ..., xn are the elements of this set.