---
# System prepended metadata

title: Untitled

---

Implement a minimal `Option` that has `map` and `flatMap`.
```scala
import scala.{Option => _}
// implement here

sealed abstract class Option[+T] { self =>
    def map[B](f: T => B): Option[B] =
        this match {
            case Some(x) => Some(f(x))
            case None => None
        }
    def flatMap[B](f: T => Option[B]): Option[B] =
        this match {
            case Some(x) => f(x)
            case None => None
        }
}
final case class Some[T](t: T) extends Option[T]
final case object None extends Option[Nothing]


val maybeInt: Option[Int] = ???


val maybeInt: Option[String] = (Some(1): Option[Int]).map(_.toString)
val maybeInt2 = None.flatMap(i => if (i > 0) Some(i) else None)


sealed trait Animal
case object Dog extends Animal

Dog <: Animal
val maybeDog: Option[Animal] = Some(Dog)

Dog <: Animal =>
Option[Dog] <: Option[Animal]

Any <: String ??
String <: Any ??
Option[T] <: Option[Any]


Bottom <: Animal
Option[Bottom] <: Option[Animal]

Nothing <: T

val maybeDog: Option[Animal] = None: Option[Nothing] // Option[?] <: Option[Animal] => ? <: Animal
val maybeString: Option[String] = None

