• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Using Apply and Unapply Methods in Scala

July 7, 2012 //  by Mohamed Sanaulla//  Leave a Comment

Before we proceed to learn about Apply and Unapply methods, its good to know what a companion object is. A companion object in Scala for some class say Fraction is defined as:

 
class Fraction(var numerator:Int, var denominator:Int){

  def *(fraction2:Fraction) = {

    Fraction(this.numerator*fraction2.numerator,
             this.denominator*fraction2.denominator)
  }

  override def toString = this.numerator+"/"+this.denominator
}

//the below construct is the companion object for Fraction class.
object Fraction{

}

One can provide apply(args) and unapply(args) methods in the companion objects which can be used implemented to some special operations in Scala. Suppose for example you wish to create a Fraction instead as below:

 
val fraction1 = Fraction(3,4)

the above line behind the scenes invokes the apply method provided in the companion object for Fraction. Similarly suppose I want to extract the contents of an instance and assign them to another object, something like:

 
val fract1 = Fraction(3,4)
val fract2 = Fraction(2,4)
val Fraction(numer, denom) = fract1 * fract2
println("Numerator: "+numer+" Denominator: "+denom)

the above code invokes the unapply() method behind the scenes.

In the apply method we take the required parameters and return the new instance of class we are interested in where as we do the reverse in unapply- we take the instance and extract out the required information and return them in the form of a tuple.

Lets add those methods in the Fraction object

 
object Fraction{

  def apply(numer:Int, denom:Int) = new Fraction(numer,denom)

  def unapply(fraction:Fraction) = {
    if ( fraction == null ) None
    else Some(fraction.numerator, fraction.denominator)
  }

}

Check this out if you are curious about Some and None. The idea is simple- in apply method take the parameter required and then return a new Fraction instance and in the unapply extract the values in the fields of the object and then return those values. Lets see these things in action:

 

object Main extends App{

  println(Fraction(3,4) * Fraction(2,4) )

  val Fraction(numer, denom) = Fraction(1,4) * Fraction(4,5)

  println("Numerator: "+numer+" Denominator: "+denom)
}

The output:

6/16
Numerator: 4 Denominator: 20

Lets see other examples of how the apply and unapply methods can be used. There’s another method called: unapplySeq which can be used to extract an arbitrary sequence of values. We can use unapply to extract any data from the given data. This is particularly useful in pattern matching where Case classes as used. Case classes already implement these apply and unapply methods.

 
object Name{
  def unapply(input:String) = {
    val pos = input.indexOf(" ")
    if ( pos == -1 ) None
    else Some(input.substring(0,pos), input.substring(pos+1))
  }
}

object Main extends App{

  val personName = "FirstName LastName"
  val Name(firstName, lastName) = personName
  println(firstName+" Last: "+lastName)

}

In the above code we extract the first name and last name from the given name assuming the first and last name are made up of one word respectively.

also read:

  • Primary and Auxiliary Constructors in Scala
  • Traits in Scala
  • Java HelloWorld vs Scala HelloWorld

Category: ScalaTag: scala

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Previous Post: « Traits in Scala- Advanced concepts
Next Post: Functions as First Class Citizens in Scala »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact