Why does the definition of w60> val with a point in it cause an error at a later stage than parsing?

In response to a comment from another question, I tried putting this code in Scala:

trait Foo

new Foo { self =>
  val self.x = 3
}

It does not compile, of course, but the error puzzles me:

recursive value x$1 needs type
    val self.x = 3
             ^

How did this code go through the parser?

-Xprint:parse also looks weird:

    <synthetic> private[this] val x$1 = 3: @scala.unchecked match {
      case self.x => ()
    }

Is this matchin type annotation for 3? edit: Apparently not; what's the syntax of annotations .

+4
source share
1 answer

Scala definition variables are actually pattern matching. That is, when you write

val x = y
println(x)

This is basically the same as writing

y match {
  case x =>
    println(x)

This can be easily seen in things like this:

val List(a, b, c) = someList
val RegexPattern(year, month, date) = "2013-10-23"

, - :

object X {
  val One = 1
}

scala> val X.One = 1

scala> val X.One = 2
scala.MatchError: 2 (of class java.lang.Integer)

:

object Y {
  val ymd = """(\d\d\d\d)-(\d\d)-(\d\d)""".r
}

scala> val Y.ymd(year, month, day) = "2013-10-23"
year: String = 2013
month: String = 10
day: String = 23    

, , , , .

+7

All Articles