Scala Extractors

Scala’s extractors and unapply() represent a concept that I haven’t grok’ed until now.

Most demonstrations seem to be too long or use the same types everywhere. Here are my thoughts on the concept;

val a: C = ???
val i: I = ???

i match {
    case a(f: O) =>

This will try to call the below method

class C {
    def unapply(i: I): Option[O] =

There’s some more flexibility;

  • the I in unapply(i: I) can be <: I
  • you can do the @ matching by returning Boolean
  • there’s the super weird infix notation

… but nothing that seems challenging anymore. Methods are chosen by their name and parameters, so you can’t have two methods which are different by their return type only. The compiler handles unpacking into tuples … I think … and values that are tested but not captured … seem … sensible.

Peter LaValle avatar
About Peter LaValle
Peter is currently a PhD student at the University of Nottingham. His day work involves applying functional programming to problems with artificial intelligence. Someday maybe he'll retire and be a graphics programmer or demoscene coder.
comments powered by Disqus