What does [+ A] mean in a Scala class declaration?

In scala, a class class is declared as

  sealed abstract class _Option[+A]

  case object _None extends _Option[Nothing] {}

  final case class _Some[+A](x: A) extends _Option[A] {}

What is [+A]? Why not easy [A]? Could this be [-A]and what will it mean?

Sorry if this is a duplicate, but I could not find an answer to SO.

+4
source share
1 answer

He declares the class covariant in its general parameter. For your example, this means what Option[T]is a subtype Option[S]if it Tis a subtype S. So, for example, it Option[String]is a subtype Option[Object]that allows you to:

val x: Option[String] = Some("a")
val y: Option[Object] = x

Conversely, a class can be contravariant in its general parameter if it is declared as -A.

Scala .

+4

All Articles