Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Some and Option in Scala

Tags:

scala

I am reading this example from their docs:

class Email(val username: String, val domainName: String)

object Email {
  def fromString(emailString: String): Option[Email] = {
    emailString.split('@') match {
      case Array(a, b) => Some(new Email(a, b))
      case _ => None
    }
  }
}

println(Email.fromString("[email protected]"))
val scalaCenterEmail = Email.fromString("[email protected]")
scalaCenterEmail match {
  case Some(email) => println(
    s"""Registered an email
       |Username: ${email.username}
       |Domain name: ${email.domainName}
     """)
  case None => println("Error: could not parse email")
}

My questions:

  1. What is Some and Option?

  2. What is a factory method (just some function that creates a new object and returns it?)

  3. What is the point of companion objects? Is it just to contain functions that are available to all instances of class? Are they like class methods in Ruby?

like image 703
Jwan622 Avatar asked Apr 20 '18 15:04

Jwan622


People also ask

What does some () mean in scala?

Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some.

What is _ and _1 in scala?

_1 is: _ = wildcard parameter _1 = first parameter in method parameter list But when used together with .

Which are the forms of the option class in scala?

filter – Returns the Option if the Option's value is Some and the supplied function returns true. filterNot – Returns the Option if the Option's value is Some and the supplied function returns false. exists – Returns true if the Option is set and the supplied function returns true.

What is the meaning of => in scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .


1 Answers

What is Some and Option?

Option is a data structure that represents optionality, as the name suggests. Whenever a computation may not return a value, you can return an Option. Option has two cases (represented as two subclasses): Some or None.

In the example above, the method Email.fromString can fail and not return a value. This is represented with Option. In order to know whether the computation yielded a value or not, you can use match and check whether it was a Some or a None:

Email.fromString("[email protected]") match {
  case Some(email) => // do something if it's a Some
  case None => // do something it it's a None
}

This is much better than returning null because now whoever calls the method can't possibly forget to check the return value.

For example compare this:

def willReturnNull(s: String): String = null

willReturnNull("foo").length() // NullPointerException!

with this

def willReturnNone(s: String): Option[String] = None

willReturnNone("foo").length() // doesn't compile, because 'length' is not a member of `Option`

Also, note that using match is just a way of working with Option. Further discussion would involve using map, flatMap, getOrElse or similar methods defined on Option, but I feel it would be off-topic here.

What is a factory method (just some function that creates a new object and returns it?)

This is nothing specific to Scala. A "factory method" is usually a static method that constructs the value of some type, possibly hiding the details of the type itself. In this case fromString is a factory method because it allows you create an Email without calling the Email constructor with new Email(...)

What is the point of companion objects? Is it just to contain functions that are available to all instances of class? Are they like class methods in Ruby?

As a first approximation, yes. Scala doesn't have static members of a class. Instead, you can have an object associated with that class where you define everything that is static.

E.g. in Java you would have:

public class Email {
   public String username;
   public String domain;
   public static Optional<Email> fromString(String: s) {
      // ...
   }
}

Where as in Scala you would define the same class as roughly:

class Email(val username: String, val domain: String)
object Email {
  def fromString(s: String): Option[Email] = {
    // ...
  }
}
like image 122
Gabriele Petronella Avatar answered Oct 31 '22 00:10

Gabriele Petronella