Why is the position of the type of method marked as negative?

Sorry, I asked a few questions, like this one, but I still can’t get a clear answer, maybe my bad English and fuzzy expression puzzled good people.

When I read "Type Parameterization" in this article: http://www.artima.com/pins1ed/type-parameterization.html , I see that there are some explanations regarding type positions:

As a far-fetched example, consider the following class definition, where a variation of several positions is annotated using + (for positive) or ^ - (for negative):

   abstract class Cat[-T, +U] {
     def meow[W^-](volume: T^-, listener: Cat[U^+, T^-]^-)
     : Cat[Cat[U^+, T^-]^-, U^+]^+
   }

I can understand most of this class except position W. I do not understand why it is designated as negative , and there is no explanation in the whole document.

It also says:

Type parameters annotated with + can only be used in positive positions, and type parameters annotated with - can only be used in negative positions.

How can I find a type with annotation -in a position Wto match this negative position?

+3
source share
2 answers

The language reference says:

  • The dispersion position of the parameter of the method is opposite to the dispersion position of the proposal to include the parameter.
  • .
  • .

, , ?

class Moo[+A, -B] {
  def foo[X] (bar : Y) ...

, Y , . B , A.

, X ? A B - , !

, , , . X , . X, :

class Moo[+A, -B] {
  def foo[X >: Z] (bar : B) ...

, , Z A, B, ? , Z X, X , , Z . :

abstract class Moo[+A, -B] {
      def foo[X >: A] (bar : B)
}    
defined class Moo

, !

+4

:

http://www.scala-lang.org/files/archive/spec/2.11/04-basic-declarations-and-definitions.html#variance-annotations

Sequence.append - 4.5.2 pdf, .

abstract class Sequence[+A] {
  def append[B >: A](x: Sequence[B]): Sequence[B]
}

. Seq.++, " " " ", .

, , Option.getOrElse, , , , .

, :

Seq[Fruit], Seq[Orange]. Apple <: Fruit, Seq[Apple] .

B . B , B .

, :

scala> trait X { def append[-](): Unit }
defined trait X
+1

Source: https://habr.com/ru/post/1538664/


All Articles