What does A # B mean in scala

I am trying to understand the following piece of code. but I don't know what R # X means. can someone help me?

// define the abstract types and bounds trait Recurse { type Next <: Recurse // this is the recursive function definition type X[R <: Recurse] <: Int } // implementation trait RecurseA extends Recurse { type Next = RecurseA // this is the implementation type X[R <: Recurse] = R#X[R#Next] } object Recurse { // infinite loop type C = RecurseA#X[RecurseA] } 
+4
source share
1 answer

You can get the type from an existing instance of the class:

 class C { type someType = Int } val c = new C type t = c.someType 

Or it can access the type directly without instantiating the object: C#someType This form is very useful for type expressions where you do not have space to create intermediate variables.


Adding some clarification, as suggested in the comments.

Disclaimer: I only have a partial understanding of how a system like Scala works. I tried to read the documentation several times, but I was able to extract only ambiguous knowledge from it. But I have rich experience in Scala and you can well predict the behavior of compilers on individual occasions.

# named projection type and projection type complement the usual access to the hierarchical type through . Each Scala expression implies the use of both.

The scala link gives examples of such invisible conversions:

 t ə.type#t Int scala.type#Int scala.Int scala.type#Int data.maintable.Node data.maintable.type#Node 

As you can see, any trivial use of type projection actually works on the type (i.e. return with .type ), and not on the object. The main practical difference (I'm bad with definitions) is that the type of the object is something ephemeral than the object itself. Its type can be changed in appropriate circumstances, such as inheriting the type of an abstract class. The type of contrast type (definition of projection type) is stable as the sun. Types (do not mix them with classes) in Scala are not first-class citizens and can no longer be overridden.

There are various places that are suitable for entering type expressions. There are also some places where only persistent types are allowed. Thus, basically the type of projection is more constant for type terms.

+6
source

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


All Articles