Weird inheritance

Showing 1-4 of 4 messages
Weird inheritance Nils 12/1/13 1:42 PM

I just found the equivalent of this in my code base:

case class Foo(bar: Int) extends {
  override def toString = bar.toString
}

Not sure how extends ever made it in there (probably a refactor leftover), but it does bring up a few questions:

  1. Is this valid syntax (per the spec)?
  2. Is there any difference if extends is removed?

If valid, I assume Foo is extending an anonymous class, but classOf[Foo].getSuperclass just yields Object.

Weird inheritance Benjamin Jackman 12/5/13 8:26 PM
Re: [scala-user] Weird inheritance som-snytt 12/5/13 10:34 PM
It's not an early definition, notice the `with`:

scala> new { override def toString = "oops" } with AnyRef
<console>:1: error: only concrete field definitions allowed in early object initialization section
       new { override def toString = "oops" } with AnyRef
                          ^


Where is the one-question FAQ when you really need it?

From the book:

trait Greeting {
  val name: String
  val msg = "How are you, "+name
}
class C extends {
  val name = "Bob"
} with Greeting {
  println(msg)
}


From the compiler:

  lazy val syntaxAnalyzer = new {
    val global: Global.this.type = Global.this
  } with SyntaxAnalyzer {
    val runsAfter = List[String]()
    val runsRightAfter = None
    override val initial = true
  }


The strange syntax is the optional extends:  ‘extends’ ClassTemplate | [[‘extends’] TemplateBody]

where AnyRef is understood.  So it's optional.

Why is it indeed so strange?

If only there were this usage of the underscore:

class Foo extends _ {
  // stats
}




On Thu, Dec 5, 2013 at 8:26 PM, Benjamin Jackman <benjamin...@gmail.com> wrote:
I believe that is an early initializer.

http://stackoverflow.com/questions/4712468/in-scala-what-is-an-early-initializer

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Re: [scala-user] Weird inheritance Simon Ochsenreither 12/6/13 5:12 AM

The strange syntax is the optional extends:  ‘extends’ ClassTemplate | [[‘extends’] TemplateBody]

where AnyRef is understood.  So it's optional.

Should we get rid of that?