Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Something like Option (Some, None) but with three states: Some, None, Unknown

I need to return values, and when someone asks for a value, tell them one of three things:

  1. Here is the value
  2. There is no value
  3. We have no information on this value (unknown)

case 2 is subtly different than case 3. Example:

val radio = car.radioType
  1. we know the value: return the radio type, say "pioneer"
  2. b. there is no value: return None
  3. c. we are missing data about this car, we don't know if it has a radio or not

I thought I might extend scala's None and create an Unknown, but that doesn't seem possible.

suggestions?

thanks!

Update:

Ideally I'd like to be able to write code like this:

car.radioType match { 
   case Unknown => 
   case None => 
   case Some(radioType : RadioType) => 
}
like image 901
Alex Black Avatar asked Nov 15 '09 16:11

Alex Black


People also ask

What are option some and none in Scala?

An Option[T] can be either Some[T] or None object, which represents a missing value. For instance, the get method of Scala's Map produces Some(value) if a value corresponding to a given key has been found, or None if the given key is not defined in the Map.

What is option () in Scala?

The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.

How do I know if Scala is none?

We can test whether an Option is Some or None using these following methods: isDefined – true if the object is Some. nonEmpty – true if the object is Some. isEmpty – true if the object is None.

Which data type does Scala use instead of NULL for optional values?

In Scala, using null to represent nullable or missing values is an anti-pattern: use the type Option instead. The type Option ensures that you deal with both the presence and the absence of an element. Thanks to the Option type, you can make your system safer by avoiding nasty NullPointerException s at runtime.


3 Answers

Here's a barebones implementation. You probably want to look at the source for the Option class for some of the bells and whistles:

package example

object App extends Application {
  val x: TriOption[String] = TriUnknown

  x match {
    case TriSome(s) => println("found: " + s)
    case TriNone => println("none")
    case TriUnknown => println("unknown")
  }
}

sealed abstract class TriOption[+A]
final case class TriSome[+A](x: A) extends TriOption[A]
final case object TriNone extends TriOption[Nothing]
final case object TriUnknown extends TriOption[Nothing]
like image 169
Mitch Blevins Avatar answered Oct 02 '22 07:10

Mitch Blevins


Don't tell anyone I suggested this, but you could always use null for Unknown rather than writing a new class.

car.radioType match { 
   case null => 
   case None => 
   case Some(radioType : RadioType) => 
}
like image 24
psp Avatar answered Oct 02 '22 05:10

psp


You can grab some stuff from Lift: the Box. It has three states, Full, Failure and Empty. Also, Empty and Failure both inherit from EmptyBox.

like image 45
Daniel C. Sobral Avatar answered Oct 02 '22 06:10

Daniel C. Sobral