Question

I use scala 2.10:

does any know what is the meanning of:

type Session >: Null <: SessionDef

I do not understand character >: and <: in here

the whole code is:

trait DatabaseComponent { self =>

type Session >: Null <: SessionDef

}
Was it helpful?

Solution

The type keyword defines some abstract type. A specific DatabaseComponent subclass (a concrete implementation) can implement this type as a concrete class.

The >: and <: are type bounds on the abstract type Session. The ones in the example mean that an abstract type member Session is null-able and it is a subtype of some other type SessionDef.

The upper bound SessionDef (<:) is here because:

  • an implementation of DatabaseComponent can only define Session types such that they are subtypes of SessionDef
  • if some method in DatabaseComponent happens to return an object of type Session, you won't know its exact implementation, but you will know that you can call whatever method exists in SessionDef on it

The lower bound Null (>:) is here because it allows you to initialize variables of type Session with a null value.

Read more about abstract type members here and type bounds here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top