score:0

The arg is evaluated in a context outside the current definition, so no.

You have to put the computation in another object.

If you were thinking this, the answer also turns out to be no:

scala> class P(name: String)
defined class P

scala> trait Prop { def prop(k: String) = sys.props(k) }
defined trait Prop

scala> class C(p: String = C.prop("user.name")) extends P(p); object C extends C() with Prop
<console>:9: error: module extending its companion class cannot use default constructor arguments
       class C(p: String = C.prop("user.name")) extends P(p); object C extends C() with Prop
                                                                               ^

That's because default args are methods defined by the companion.

Similarly,

scala> class C(p: String) extends P(p); object C extends C(C.prop("user.name")) with Prop
<console>:9: error: super constructor cannot be passed a self reference unless parameter is declared by-name
       class C(p: String) extends P(p); object C extends C(C.prop("user.name")) with Prop
                                                           ^

score:0

If I dont misunderstand this :), I think there are 2 things are impossible here.

  1. trait composition or stackable trait is always make right trait wins. In this example, it tries to use left one override the right one.

  2. when we use trait composition, trait structure would not change. The only flexible thing we can do is the sub-trait polymorphism. Cake pattern is using this way, but linearization is a problem. However, it is not related to this.

I think the correct way to do this, is to create a class/trait to do the override thing

class P(name:String)

trait SysConfig {
  def prop(key:String) = System.getProperty(key)
}

class C extends P("123") with SysConfig {
  override def prop(key: String) = "123"
}

trait Foo extends P with SysConfig {
  override def prop(key: String) = "123"
}

new C 

Related Query

More Query from same tag