Overriding a lazy val and super?

1,027 views
Skip to first unread message

DFectuoso

unread,
Jul 8, 2009, 2:46:15 AM7/8/09
to Lift
I'm getting a little bit confused by the override lazy val behavior so
I wanted to ask you guys...

I want to create a trait that extends the MetaMegaProtoUser and
overrides the sitemap(defined as a lazy val), adding something to the
original sitemap, how would that declaration work?

If I do something like:
override lazy val sitemap = super.sitemap :: other elements

The compiler tells me that super cannot be used in this context. If I
dont user super, then it says that its a recursive declaration(and it
seems to be). How can you achieve this overriding?

Naftoli Gugenhem

unread,
Jul 8, 2009, 6:54:29 AM7/8/09
to santia...@gmail.com, lif...@googlegroups.com
Override with a def?

-------------------------------------

David Pollak

unread,
Jul 8, 2009, 1:23:32 PM7/8/09
to lif...@googlegroups.com
trait Foo {
  def bar: String
}

trait MegaFoo extends Foo {
  lazy val bar = "Hello"
}

trait SuperMegaFoo extends MegaFoo {
  override lazy val bar = "Super Duper"
}

println((new SuperMegaFoo{}).bar)  // Super Duper
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

DFectuoso

unread,
Jul 8, 2009, 10:30:02 PM7/8/09
to Lift
But I want to override... and add something ("adding something to the
original sitemap, ")
Allright, with your example I can express my question perfectly!

If you have
trait Foo {
def bar: String
}

trait MegaFoo extends Foo {
lazy val bar = "Hello"
}

And then want to create a trait that extends MegaFoo and override bar,
adding something to that string... i get the next results:

With:
trait SuperMegaFoo extends MegaFoo {
override lazy val bar = bar + "Super Duper"
}
Compiler:
(fragment of overrideTest.scala):10: error: recursive lazy value bar
needs type
override lazy val bar = bar + "Super Duper"

With:
trait SuperMegaFoo extends MegaFoo {
override lazy val bar:String = bar + "Super Duper"
}

Compiler: (Crashhhh!)
java.lang.StackOverflowError
at scala.StringBuilder.append(StringBuilder.scala:242)
at scala.StringBuilder.<init>(StringBuilder.scala:54)
at scala.StringBuilder.<init>(StringBuilder.scala:41)
at Main$$anon$2$SuperMegaFoo$class.bar((virtual file):14)
at Main$$anon$2$$anon$1.bar((virtual file):17)
at Main$$anon$2$SuperMegaFoo$class.bar((virtual file):14)
at Main$$anon$2$$anon$1.bar((virtual file):17)
at Main$$anon$2$SuperMegaFoo$class.bar((virtual file):14)

With:
trait SuperMegaFoo extends MegaFoo {
override lazy val bar:String = super.bar + "Super Duper"
}

Compiler:
(fragment of overrideTest.scala):10: error: super may be not be used
on lazy value bar
override lazy val bar:String = super.bar + "Super Duper"

With
trait SuperMegaFoo extends MegaFoo {
override val bar:String = super.bar + "Super Duper"
}
Compiler:
(fragment of overrideTest.scala):10: error: super may be not be used
on lazy value bar
override val bar:String = super.bar + "Super Duper"

Finally, dropping the lazzy:
trait SuperMegaFoo extends MegaFoo {
override val bar:String = bar + "Super Duper"
}
Compiler:
(fragment of overrideTest.scala):10: error: error overriding lazy
value bar in trait MegaFoo of type java.lang.String;
value bar must be declared lazy to override a concrete lazy value

So this is my trouble(i get all this cases if i try to override
sitemap from the metaprotouser to add new items to the sitemap) i cant
drop the lazy, i cant use super in lazy so what am I doing something
wrong?


On Jul 8, 10:23 am, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> Beginning Scalahttp://www.apress.com/book/view/1430219890

David Pollak

unread,
Jul 9, 2009, 11:44:32 AM7/9/09
to lif...@googlegroups.com
You cannot call super on a lazy val.  Sorry.
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Reply all
Reply to author
Forward
0 new messages