This page is no longer maintained — Please continue to the home page at www.scala-lang.org

override on trait

2 replies
Oliver Lambert
Joined: 2009-01-04,
User offline. Last seen 42 years 45 weeks ago.

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

marius
Joined: 2008-08-31,
User offline. Last seen 3 years 19 weeks ago.
Re: override on trait
Using mixins like

new OneTrait with TwoTrait {
  ...
}

?


Br's,
Marius

On Sun, Jan 4, 2009 at 2:43 AM, Oliver Lambert <olambo [at] gmail [dot] 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

Oliver Lambert
Joined: 2009-01-04,
User offline. Last seen 42 years 45 weeks ago.
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:
Using mixins like

new OneTrait with TwoTrait {
  ...
}

?


Br's,
Marius

On Sun, Jan 4, 2009 at 2:43 AM, Oliver Lambert <olambo [at] gmail [dot] 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


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland