Right arrow in body class in Scala

Looking through the Scala source code, I came across Enumeration.scala :

 abstract class Enumeration(initial: Int, names: String*) extends Serializable { thisenum => def this() = this(0) def this(names: String*) = this(0, names: _*) /* Note that `readResolve` cannot be private, since otherwise the JVM does not invoke it when deserializing subclasses. */ protected def readResolve(): AnyRef = thisenum.getClass.getField("MODULE$").get() // ... SNIP ... } 

What is thisenum => for? I could not find the information in the book "Programming in Scala".

+6
scala
source share
3 answers

Programming in the Scala 2d editor introduces the concept of type self in section 29.4 "Separation of modules into a sign":

A SimpleFoods feature might look like this:

 trait SimpleFoods { object Pear extends Food("Pear") def allFoods = List(Apple, Pear) def allCategories = Nil } 

So far, so good, but unfortunately, the problem arises if you try to define the SimpleRecipes property as follows:

 trait SimpleRecipes { // Does not compile object FruitSalad extends Recipe( "fruit salad", List(Apple, Pear), // Uh oh "Mix it all together." ) def allRecipes = List(FruitSalad) } 

The problem here is that Pear is in a different sign from using it, so it goes beyond that.
The compiler does not know that SimpleRecipes only mixes with SimpleFoods .
However, you can tell this to the compiler. Scala provides a self type for this situation.
Technically, the self type is the intended type for this when it is mentioned in the class .
Pragmatically, the self type sets requirements for any particular class; this attribute is mixed with .
If you have a trait that is used only when it is mixed with another trait or traits, you can indicate that these other traits should be used.
In this case, it is enough to specify the self type SimpleFoods , as shown:

 trait SimpleRecipes { this: SimpleFoods => object FruitSalad extends Recipe( "fruit salad", List(Apple, Pear), // Now Pear is in scope "Mix it all together." ) def allRecipes = List(FruitSalad) } 

Given the new type of self, Pear is now available.
Implicitly, a link to Pear is considered this.Pear .
This is safe because any particular class that mixes in SimpleRecipes must also be a subtype of SimpleFoods , which means Pear will be a member.
Abstract subclasses and traits should not follow this restriction, but since they cannot be created using the new one, there is no danger that this.Pear reference this.Pear not be executed

+6
source share

This is the type itself. See Programming Section 29.4 in Scala Second Edition. I do not think this was considered in the first edition, and I have no one who could find it.

All that was in this example was that thisenum would reference Enumeration this to any inside of Enumeration .

+2
source share

This is not an annotation of type self, but simply an alias for this , since there is no type requirement in the question, check this SO question

+1
source share

All Articles