Scala: ‘Apply’ & ‘Unapply’ Class Methods
By: Micah Lewis
Tags: Programming, Coding, Scala, Beginner

In Scala, the apply and unapply methods are pretty important. Some classes have these built-in automatically [I’m looking at you, ‘case class’], while in others they need to be explicitly written. This post is going to talk about what these two very important methods are and what they do.

APPLY

The apply method ties the parameters to the actual instance of the class or case class. Below is an example:

class Person {

var name: Option[String] = None

var age: Option[Int] = None

} object Person {

def apply = (name: Option[String], age:Option[Int]): Person = {

var p = New Person

p.name = name

p.age = age

p

}

}

Above you can see that we create the class Person with two attributes, “name” and “age”. Below that is the object [This is technically called a companion object in Scala. I’ll create a post about this later, but for now just know that a companion object is where you should place all of your constructors and class methods, whereas the class itself should only have attributes.] that with the apply method.

This method makes it so that whenever you create something like:

val me = new Person(name: "Micah", age: 31)

it sets the name and age to “Micah” and 31 respectively for the me value.

UNAPPLY

While the apply method ties the parameters of the new object you create to that object, the unapply method takes them apart. That sounds weird, I know, but stick with me. Lets take the following code below:

class Person {

val name: Option[String]

val age: Option[Int]

} object Person {

def unapply(p: Person): String = s"${p.name}, ${p.age}"

} val me = new Person("Micah", 31) Person.unapply(me) // returns "Micah, 31"

In the above example, take a look at the unapply method for just a moment. All I did was tell it to return all of the attributes as a string, which I show on the last line, however you don’t have to have your unapply method return a string. You can have it return a List or a Tuple or anything. You can have functionality to slice up the information within each attribute as well. Really the sky is the limit here.

While I say that the sky is the limit, the unapply method is also the floor as well because this method is mandatory for all classes in Scala. Why you ask? Well because Scala is so reliant on using case matching, it needs the unapply method to return all attributes in your data so that it can use match against different cases in your code. Apply on the other hand is technically an optional method.

Some last little tidbits here to wrap up:

You might hear people refer to apply as an “injection method” as it technically injects the attributes into your object.

You might hear people refer to unapply as an “extraction method” as it technically pulls the attributes out in some orderly way [Usually a list or tuple]

Case classes [which are special to Scala, I’ll write a post about them later as they are very cool. If you don’t know what they are, don’t worry.] already have an apply and unapply built in to them, so you don’t need to define the method yourself. When you call unapply on a case class, it will return an Option with the values and types intact.

Thanks for reading! If you have any questions, or spotted any errors, please don’t hesitate to reach out to me below!