Why is `trait X {def append [-] (): Unit}` compiled?

At the bottom of this answer: https://stackoverflow.com/a/318616/169 , there is some code that looks weird:

trait X { 
  def append[-](): Unit 
}

Why can it be compiled? I mean [-]weird

+4
source share
1 answer

This is strange, but in this context -is an acceptable identifier for a type parameter. Here is a longer example:

class Y {
  def identity[-](x: -): - = x
}
(new Y).identity(5) // returns 5

-inside [-]here is the normal type name, as well -as the class name in the following code:

class -

, , - . , :

class Z[-] {}
+7

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


All Articles