Nathaniel Ford
Nathaniel Ford

Reputation: 21220

Naked 'extends' keyword

I was puzzling through the Lift Cookbook for AJAX Forms, and I ran across the following object declaration:

object EchoForm extends {

This was confusing, so I tried it out, and it compiled fine. My Eclipse IDE doesn't seem to indicate that any additional features were inherited, but I suppose I don't trust keywords that are just 'hanging out'. Does this 'naked' extends do anything, or is it parsed as 'extends nothing in particular'?

Upvotes: 9

Views: 235

Answers (3)

senia
senia

Reputation: 38045

It's not an early object initialization section! See this answer.

There should be Parent for early object initialization, but in code sample from the book there is no parents:

object EchoForm extends {
  def render = {
    ...
  }
} // no parents here!

Old answer (before @som-snytt mentioned it's wrong):

It's It could be (with parents) an early object initialization section. Take a look at this example:

trait Test {
  val i: Int
  val j = i + 1
}

Wrong creation of an instance:

object TestObj extends Test { val i = 1 }
TestObj.j
// Int = 1

j is initialized before i, but j depends on i.

Correct creation:

object TestObj extends { val i = 1 } with Test
TestObj.j
// 2

Early object initialization section allows initialize fields before all fields from inherited traits.

Upvotes: 11

som-snytt
som-snytt

Reputation: 39577

This just came up on the ML:

https://groups.google.com/forum/#!topic/scala-user/_qMoODIBQtE

followed by an itchy finger to deprecate the syntax:

https://groups.google.com/d/msg/scala-internals/8zlyUH3S7sU/0EFiLSx9B68J

Here is the link to the syntax:

https://github.com/scala/scala-dist/blob/2.10.x/documentation/src/reference/SyntaxSummary.tex#L272

Basically, object Foo { } is the same as object Foo extends { }.

Footnote: the snippet in question is withless:

object EchoForm extends {
  def render = {
    //snip
  }
}

Upvotes: 5

Glen Best
Glen Best

Reputation: 23105

Does this 'naked' extends do anything, or is it parsed as 'extends nothing in particular'?

It extends the 'inline' anonymous class immediately after the word extends:

object EchoForm extends <definition of anonymous class here - within brackets, optionally followed by `with` clauses for trait mixins>

Upvotes: 2

Related Questions