Matching the pattern takes input and decomposes it using the unapply function. Thus, in your case, unapply(4) will have to return two numbers, the sum of which is 4. However, there are many pairs that add up to 4, so the function does not know what to do.
You need 2 be available for unapply function. For this, the special case class will work, in which 2 is stored:
case class Sum(addto: Int) { def unapply(i: Int) = Some(i - addto) } val Sum2 = Sum(2) val Sum2(x) = 5
(It would be nice to do something like val Sum(2)(y) = 5 for compactness, but Scala does not allow parameterizing extractors, see here ).
[EDIT: This is a little silly, but you can do the following too:
val `2 +` = Sum(2) val `2 +`(y) = 5
]
EDIT: The reason head::tail works is because there is one way to split the head into the tail of a list.
There is nothing special about :: versus + : you could use + if you had a predetermined idea of how you wanted it to break the number. For example, if you want + mean "split in half," you could do something like:
object + { def unapply(i: Int) = Some(ii/2, i/2) }
and use it like:
scala> val a + b = 4 a: Int = 2 b: Int = 2 scala> val c + d = 5 c: Int = 3 d: Int = 2
EDIT: Finally, this explains that when matching pattern A op B means the same as op(A,B) , which makes the syntax nice.