简体   繁体   中英

what does A#B mean in scala

I am trying to understand the following piece of code. but I don't know what the R#X mean. could 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]
}

You may gain type from existing instance of a class:

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

Or may address to the type directly without instantiating an object: C#someType This form is very usefull for type expressions where you have no space to create intermediate variables.


Adding some clarifications as it was suggested in comments.

Disclaimer : I have only partial understanding of how Scala's type system works. I'd tried to read documentation several times, but was able to extract only patchy knowledges from it. But I have rich experience in scala and may predict compilers behavior on individual cases well.

# called type projection and type projection compliments normal hierarchical type access via . In every type expression scala implicitly uses both.

scala reference 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 use see, every trivial usage of type projection actually works on type (that is return with .type ) not on an object. The main practical difference (I'm bad with definitions) is that object type is something ephemeral as object itself is. Its type may be changed in appropriate circumstances such as inheritance of an abstract class type. In contrast type's type (the definition of the type projection) is as stable as sun. Types (don't mix them with classes) in scala are not first-class citizens and can not be overridden further.

There are different places suitable for putting type expression into. There are also some places where only stable types are allowed. So basically type projection is more constant for terms of type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM