3

I'm looking into the scala actors library, and there I found the following code:

private[scheduler] trait TerminationMonitor { 
    _: IScheduler =>

    protected var activeActors = 0 
    ...

The question is what is the meaning of _: IScheduler => is here?
I'm new to Scala and it confuses me that there are so many different meanings with the underscore.

Thanks for your help in advance!

ZelluX
  • 69,107
  • 19
  • 71
  • 104
K J
  • 4,505
  • 6
  • 27
  • 45
  • 2
    possible duplicate of [What does "outer =>" really mean?](http://stackoverflow.com/questions/4353915/what-does-outer-really-mean) or [Explicit self-references with no type / difference with ''this''](http://stackoverflow.com/questions/8073263/explicit-self-references-with-no-type-difference-with-this). The only difference is that the underscore is used here for naming the self reference which means forget the name. – kiritsuku Jun 24 '12 at 10:33
  • 1
    Found this link http://www.slideshare.net/normation/scala-dreaded which references SO – octopusgrabbus Jun 24 '12 at 10:43
  • Thanks for the useful links! These _'s really drive me crazy :/ – K J Jun 24 '12 at 13:28

1 Answers1

5

This usage of the underscore is similar to those:

someElem match {
  case _: String => doSomething()
}

val k = (_: Int) => "This does not use the Int argument."

val (m, _, o) = (1,2,3)

It is a syntactic placeholder for an identifier (variable) which is immediately discarded afterwards.

In your example, the naming of the self-type is therefore being avoided. (But since the self-type reference is always accessible as this, it would be equivalent to writing this: IScheduler => in that special case.)

Debilski
  • 66,976
  • 12
  • 110
  • 133
  • Thanks for the answer, Debilski. I didn't know about the self type declaration. :/ – K J Jun 24 '12 at 13:26