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.
trait composition or stackable trait is always make right trait wins. In this example, it tries to use left one override the right one.
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
Source: stackoverflow.com
Related Query
- How to define an object which will pass arguments to super class by calling methods?
- In Scala, how can I define a companion object for a class defined in Java?
- How to pass a class object from Scala to Java?
- How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?
- Converting Java to Scala, how to deal with calling super class constructor?
- How do I define a function def which accepts arguments and returns a value?
- In SPARK, How to define common UDF/codebase, which will be shared by all scala script
- How to pass complex Java Class Object as parameter to Scala UDF in Spark?
- How to create mock object of a class which is package private
- How to pass class object as a variable in Java
- How to pass a scala class as an object into a function parameter?
- How to create package object which will be available to all the inner packages in scala?
- How can an object access the member of super class
- scala: how to define an object with explicit class type
- how do we define the udf to pass struct which has arrays?
- How should I pass a computed object from the primary class constructor to the supertype constructor?
- what is the data type of object when passing to another class in scala ? or how to pass an object type as a parameter to another class?
- How can I define a custom equality operation that will be used by immutable Set comparison methods
- How to instantiate trait which extends class with constructor
- SLICK How to define bidirectional one-to-many relationship for use in case class
- How to pass a class method as a parameter in Scala
- How can I pass a Scala object reference around in Java?
- How to test arguments to a case class constructor?
- Given an instance of any class type, how to find out which parent class and/or traits it inherits from or implements?
- How to setup sbt/scala/play multi-module project which will work fine with Intellij scala plugin
- How to get the class of a singleton object at compile time?
- How to define a ternary operator in Scala which preserves leading tokens?
- pass variable number of arguments in scala (2.8) case class to parent constructor
- How to reference the name of a val in a Object class in scaladoc?
- How to iterate org.json4s.JsonAST.JValue which is an array of JSON objects to separately work on each object in Scala?
More Query from same tag
- Scala : Can there be covariance in a constructor for defined type?
- scalaz Monoid for SortedMap
- Dependency injection without classes Scala
- code reducing by using traits and the with-keyword
- Pattern Matching casting
- I have two scala projects - basically the same - one works and one does not. Can someone tell me why?
- Why Scala's foldLeft has lower performance than iterating with an index on strings?
- Scala Copy() Odd Behavior
- Spark streaming: How to write cumulative output?
- Add new column to dataframe based on previous values and condition
- Sending a message from one actor to another actor
- Spark Dataframe - How to get a particular field from a struct type column
- How to use SORM framework with Play Framework?
- Some help needed to help the type inferring engine
- Scala-Play: Why are the Form default values not being picked up?
- Does Scala provide an idiomatic way to partially match strings other than regular expressions?
- Scala - custom Marshaller for case class
- Scala Actor in script mode
- Cannot access value from case class
- SQL Union equivalent in Twitter Scalding
- In Scala how to compare current values with previous values while processing a stream?
- Accepting functions of Any type in scala
- How can implicit function type in scala can model effects?
- Write RDD[entity] in cassandra from Spark
- How do i define generic parameter "on the fly"?
- scala convert String to generic type
- Scala: how to use google/facebook credentials to provide access control for my application via openID on Heroku
- What does ":+" mean in Scala
- Wait in tail recursion for future to complete
- Generic Play json Formatter