Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the self => used for? [duplicate]

Tags:

scala

Possible Duplicate:
What does “outer =>” really mean?

When i look in to the source code of: scala/src/library/scala/Option.scala

sealed abstract class Option[+A] extends Product with Serializable {
  self =>

I wander what the self use for. I know the normal use of self type is to restrict the class the trait can be mixed in. such as:

scala> trait A
defined trait A

scala> trait NeedA {self: A =>}
defined trait NeedA

scala> new NeedA {}
<console>:10: error: illegal inheritance;
 self-type java.lang.Object with NeedA does not conform to NeedA's selftype NeedA with A
              new NeedA {}
                  ^

scala> new NeedA with A {}
res39: java.lang.Object with NeedA with A = $anon$1@4d04a0e8

scala>

but the "this =>" is not the case. what indeed this "this =>" used for?

like image 601
user962278 Avatar asked Dec 16 '11 10:12

user962278


People also ask

How do you duplicate in Python?

In Python, we use = operator to create a copy of an object. You may think that this creates a new object; it doesn't. It only creates a new variable that shares the reference of the original object.

What is duplicate function?

The duplicated() function is used to indicate duplicate Series values. Duplicated values are indicated as True values in the resulting Series. Either all duplicates, all except the first or all except the last occurrence of duplicates can be indicated.


1 Answers

It creates an alias to this, which may be handy in inner classes (synonymous for OuterClass.this)

class A {self => 
   ...
  class B {
      // self is the enclosing A, synonymous for A.this
  }
}
like image 162
Didier Dupont Avatar answered Oct 05 '22 16:10

Didier Dupont