5

Suppose I have a simple case class that wraps integers, and a higher order method that accepts a function carrying integers to wrappers.

case class Wrapper(x :Int)
def higherOrder(f : Int => Wrapper) = println(f(42))

Then, I can call the higher order function, passing in the wrapper's generated apply function. Amazingly, I can also just pass in the wrapper's name.

higherOrder(Wrapper.apply)  // okay
higherOrder(Wrapper)        // okay, wow!

This is really cool. It allows us to treat the name of the case class as a function, which fosters expressive abstractions. For an example of this coolness, see the answer here. What does "abstract over" mean?

Now suppose my case class isn't powerful enough, and I need to create an extractor instead. As a mildly contrived use case, let's say I need to be able to pattern match on strings that parse into integers.

// Replace Wrapper case class with an extractor
object Wrapper {
    def apply(x :Int) = new Wrapper(x)
    def unapply(s :String) :Option[Wrapper] = {
        // details elided
    }
}
class Wrapper(x :Int) {
    override def toString = "Wrapper(" + x + ")"
    // other methods elided
}

Under this change, I can still pass Wrapper.apply into my higher order function, but passing in just Wrapper no longer works.

higherOrder(Wrapper.apply)  // still okay
higherOrder(Wrapper)        // DOES NOT COMPILE
            ^^^^^^^
//    type mismatch; found : Wrapper.type (with underlying type Wrapper)
//    required: (Int) => Wrapper

Ouch! Here's why this asymmetry is troubling. The advice of Odersky, Spoon, and Venners (Programming in Scala, page 500), says

You could always start with case classes, and then, if the need arises, change to extractors. Because patterns over extractors and patterns over case classes look exactly the same in Scala, pattern matches in your clients will continue to work.

Quite true of course, but we'll break our clients if they are using case class names as functions. And since doing so enables powerful abstractions, some surely will.

So, when passing them into higher order functions, how can we make extractors behave the same as case classes?

1
  • 3
    Please don't feel embarrassed to accept an answer for any of your questions. It really is very socially acceptable, and almost everyone has done it at some point. Jan 24, 2011 at 12:00

2 Answers 2

12

Simply put, Wrapper (the object) isn't a Function, it's just an object with an apply method. This has absolutely nothing to do with the object also being an extractor.

Try this:

class Wrapper(val x: Int) {
  override def toString = "Wrapper(" + x + ")"
  // other methods elided
}

object Wrapper extends (Int => Wrapper) {
  def apply(x: Int) = new Wrapper(x)
  def unapply(w: Wrapper): Option[Int] = Some(w.x)
}

def higherOrder(f: Int => Wrapper) = println( f(42) )

I also made the parameter to Wrapper a val and swapped around the parameter and return value in your definition of unapply so that it would match case-class behaviour.

8

Scala extractor companion objects should extend Function

For case classes, the compiler generates a companion object under the hood. This holds relevant static methods for the Wrapper case class, including apply. Peering at the bytecode, we discover that Wrapper's companion object extends Function1. (Actually it extends AbstractFunction1, which uses @specialized to obviate autoboxing.)

This is also noted here. Why do case class companion objects extend FunctionN?

When replacing our Wrapper case class with an extractor, we should take care to extend our home grown companion object with AbstractFunction1 to preserve backwards compatibility for our clients.

This amounts to a small tweak in the source code. The apply method doesn't change at all.

object Wrapper extends scala.runtime.AbstractFunction1[Int, Wrapper] {
5
  • 1
    This really has nothing to do with the unapply extractor, so referring it to this as "extractor companion objects" is a little bit incorrect. I think the correct term should be "factory companion objects" (i.e. those that implement apply as a way of constructing a new object).
    – Ken Bloom
    Jan 23, 2011 at 21:17
  • @Ken, thanks for your comment. Why would I need to replace a case class with a factory companion object? It's precisely that I'm replacing a less powerful pattern matcher with a more powerful one that my problem arises. I need an extractor that's exactly equivalent to my case class, so my legacy code won't break. Jan 23, 2011 at 21:28
  • 2
    you replaced the case class with a companion object that had two functions -- one of those functions was as a factory (the apply function), and the other was as an extractor (the unapply function). Though your motivation was to improve the extractor aspect of this companion object, the problems we're discussing comes from the semantics of the factory aspect, and has nothing to do with the unapply function in the companion object. A minimal example with class C; object C{def apply(x:Int)=...} would have the same problem.
    – Ken Bloom
    Jan 23, 2011 at 21:31
  • The reason I'm being pedantic about this is to make it clear that if you created an extractor object but didn't need the factory aspect (and didn't define apply), for example object Foo{ def unapply(x:Bar):Option[Baz] } you would not need to extend FunctionN.
    – Ken Bloom
    Jan 23, 2011 at 21:35
  • Thanks, Ken. I feel that you are adding clarity, not being pedantic. Jan 25, 2011 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.