Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naked 'extends' keyword

Tags:

scala

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'?

like image 832
Nathaniel Ford Avatar asked Dec 18 '13 19:12

Nathaniel Ford


2 Answers

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.

like image 198
senia Avatar answered Sep 24 '22 18:09

senia


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
  }
}
like image 29
som-snytt Avatar answered Sep 23 '22 18:09

som-snytt