0
votes

How to create an objectthat extends a trait and uses early definition syntax?

Let's say we have two traits:

trait Trait {
  val value : String
}

trait Simple

Let's say we have also a simple class:

class Class

We can create a new object of type Class and make it extend a Simple trait:

new Class extends Simple

Is it possible to create a new object of type Class that extends the Trait and uses the early definition syntax to set value member? I tried something like:

     new Class  extends { override val value = "42" } with Trait

But this gives a syntax error:

Error:(12, 17) ';' expected but 'extends' found.
new Class extends { val value = "42" } with Trait

1
Can you clarify a bit more what you're trying to do? Why can't you just have this set up like a normal class? I suspect you want a lazy val, but can't find anything on the syntax you're referring to.Ethan
@Ethan Ooops, I meant early definition syntax. Actually, I have been just learning scala and just wondering, whether one can use that syntax when creating a new object.lukeg
Why can't you override the value in the body?sinanspd

1 Answers

0
votes
trait T { val value: String }
class C
new C with T { val value = "ok" }