146

In various Scala literature I see some self-type annotations using "this" and others using "self":

trait A { this: B => ... }
trait A { self: B => ... }

Is there any real difference between using "this" or "self"? Does it matter at all what name you use? Is this just as valid?

trait A { foo: B => ... }

2 Answers 2

186

All three forms are valid, and have the effect that B is assumed as the type of this in class A.

The first two variants

trait A { self: B => ... }
trait A { foo: B => ... }

introduce self (respectively, foo) as an alias for this in trait A. This is useful for accessing the this reference from an inner class. I.e. you could then use self instead of A.this when accessing the this reference of the trait A from a class nested within it. Example:

class MyFrame extends JFrame { frame =>    
  getContentPane().add( new JButton( "Hide" ) {
    addActionListener( new ActionListener {
      def actionPerformed( e: ActionEvent ) {
        // this.setVisible( false ) --> shadowed by JButton!
        frame.setVisible( false )
      }
    })
  })
}

The third variant,

trait A { this: B => ... }

does not introduce an alias for this; it just sets the self type.

4
  • The way I look at self type is that the trait declared itself as taking a certain type and returning the code block e.g. foo: B => {...}. Now those curlies are of course omitted. It is interesting to see that you can use the object name instead of "this" inside any scope in the code though [something we do all the time in javascript] Commented Jul 13, 2013 at 15:20
  • 4
    @Martin Odersky Is it possible to add restriction for two or more traits, something like trait A { self: B, C => ... } ?
    – Dmitry
    Commented Nov 5, 2014 at 8:57
  • 13
    @DmitryBespalov: Yes, you can use the with keyword in the self-typing annotation. For example trait A { self: B with C => ... }
    – Dave
    Commented Dec 19, 2014 at 10:11
  • 1
    btw you can also do _: B => for the non-alias case for simplicity
    – Creos
    Commented Jun 19, 2019 at 17:29
18

There is a difference in that this always refers to the object defined by the innermost template.

The expression this can appear in the statement part of a template or compound type. It stands for the object being defined by the innermost template or compound type enclosing the reference. If this is a compound type, the type of this is that compound type. If it is a template of a class or object definition with simple name C, the type of this is the same as the type of C.this. (Scala Ref. §6.5)

So, if you call your self-type foo, you could still refer to it as this (unless, of course, you are in an inner template in which case this will refer to the object defined by it – and unless you don’t give the inner template’s self-type the same name) but obviously not the other way round.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.