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:

  • Here is the meaning
  • No value
  • We have no information about this value (unknown)

case 2 is slightly different from case 3. Example:

val radio = car.radioType 
  • we know the meaning: return the type of radio, say "pioneer"
  • b. no value: return None
  • from. we don’t have enough data about this car, we don’t know if it has a radio or not.

I thought I could expand scala None and create Unknown, but this is not possible.

offers?

thanks!

Update:

Ideally, I would like to write code like this:

 car.radioType match { case Unknown => case None => case Some(radioType : RadioType) => } 
+6
scala nullable option
source share
6 answers

It uses a bubbly implementation. You probably want to see the source for the Option class for some 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] 
+12
source share

Do not tell anyone, I suggested this, but you can always use null for Unknown, and not write a new class.

 car.radioType match { case null => case None => case Some(radioType : RadioType) => } 
+8
source share

You can grab something from the Elevator: Box. It has three states: Full, Failure, and Empty. In addition, Empty and Failure are both inherited from EmptyBox.

+5
source share

You can use scala. Use Left for the exceptional value and Right for the expected value, which may be Option in this case:

 scala> type Result = Either[String, Option[String]] defined type alias Result scala> val hasValue: Result = Right(Some("pioneer")) hasValue: Result = Right(Some(pioneer)) scala> val noValue: Result = Right(None) noValue: Result = Right(None) scala> val unknownValue = Left("unknown") unknownValue: Left[java.lang.String,Nothing] = Left(unknown) 
+4
source share

You can create your own three possibilities. Or as one of your types of car.radioType, which you might not know, and then use the protective devices on your case to handle it. If you flip your own, you should also include the Product property. liftweb is of type Box, which is a lockable option that allows for full, empty and erorr.

+2
source share

I did something similar to classify 3 types of lines in a given file, for example, for a line of a line Float for a title line, Long for a line in the middle (line) or String for a trailer line. You can also use isHeader , isRow and isTrailer to find out which one. Hope it helps:

 sealed abstract class HRT[+H, +R, +T] { val isHeader: Boolean val isRow: Boolean val isTrailer: Boolean } final case class Header[+H, +R, +T](h: H) extends HRT[H, R, T] { override val isHeader: Boolean = true override val isRow: Boolean = false override val isTrailer: Boolean = false } final case class Row[+H, +R, +T](r: R) extends HRT[H, R, T] { override val isHeader: Boolean = false override val isRow: Boolean = true override val isTrailer: Boolean = false } final case class Trailer[+H, +R, +T](t: T) extends HRT[H, R, T] { override val isHeader: Boolean = false override val isRow: Boolean = false override val isTrailer: Boolean = true } object Demo { def getEntries(): Seq[HRT[Float, Long, String]] = List( Header(3.14f), Row(42), Trailer("good bye") ) val entries = getEntries() entries.foreach { case Header(f) => printf("header: %f\n", f) case Row(l) => printf("row: %d\n", l) case Trailer(s) => printf("trailer: %s\n", s) } } 
0
source share

All Articles