- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
override on trait
Sun, 2009-01-04, 01:44
Given the following code, is there anyway I can combine (make DRY) the
overrides for OneTrait and TwoTrait
object T1 {
def whatever = "whatever"
def whatever1 = "whatever1"
trait BaseTrait {
def a = whatever
def b = whatever1
}
trait OneTrait extends BaseTrait {
def apply(str: String) = {
new OneTrait {
override def a = whatever1 // overrides for OneTrait
override def b = whatever
}
}
}
trait TwoTrait extends BaseTrait {
def apply(str: String) = {
new TwoTrait {
override def a = whatever1 // overrides for TwoTrait
override def b = whatever
}
}
}
def one = new OneTrait {}
def two = new TwoTrait {}
}
cheers
Oliver
Sun, 2009-01-04, 10:47
#2
Re: override on trait
traits don't accept constructor arguments, lets say I want something a little more complex that retains a pattern in the construction between the traits, ie -
trait OneTrait extends BaseTrait { val fred "hello"
def apply(str: String) = {
new OneTrait {
override def a = whatever1+str // overrides for OneTrait
override def b = whatever+fred
}
}
}
On 04/01/2009, at 7:14 PM, Marius Danciu wrote:
trait OneTrait extends BaseTrait { val fred "hello"
def apply(str: String) = {
new OneTrait {
override def a = whatever1+str // overrides for OneTrait
override def b = whatever+fred
}
}
}
On 04/01/2009, at 7:14 PM, Marius Danciu wrote:
Using mixins like
new OneTrait with TwoTrait {
...
}
?
Br's,
Marius
On Sun, Jan 4, 2009 at 2:43 AM, Oliver Lambert <olambo@gmail.com> wrote:
Given the following code, is there anyway I can combine (make DRY) the overrides for OneTrait and TwoTrait
object T1 {
def whatever = "whatever"
def whatever1 = "whatever1"
trait BaseTrait {
def a = whatever
def b = whatever1
}
trait OneTrait extends BaseTrait {
def apply(str: String) = {
new OneTrait {
override def a = whatever1 // overrides for OneTrait
override def b = whatever
}
}
}
trait TwoTrait extends BaseTrait {
def apply(str: String) = {
new TwoTrait {
override def a = whatever1 // overrides for TwoTrait
override def b = whatever
}
}
}
def one = new OneTrait {}
def two = new TwoTrait {}
}
cheers
Oliver
new OneTrait with TwoTrait {
...
}
?
Br's,
Marius
On Sun, Jan 4, 2009 at 2:43 AM, Oliver Lambert <olambo@gmail.com> wrote: