specifying trait constraint in scala -


i want test number of stackable traits extend such 1 this:

trait layer{   def write(s:string): string } 

each 1 process incoming string somehow , passes next layer, example:

trait timestamplayer {    abstract override def write(s:string) = system.nanotime + super.write(s) }  

my idea write base test class whith such fixture:

abstract layertest {      type l     val layer = new baselayer l      // baselayer 1 nothing  }  

where each test subclass overwrite l such stackable trait. of course doesn't work, since there's no constraint on l specify it's trait. quoting compiler,

type l needs trait mixed in

i thought of like

abstract layertest[l <: layer] {   //... }  class timestamplayertest extends layertest[timestamplayer] {} 

but can't constrain l trait, or @ least don't know how to

is there way specify type trait?

the problem code more fundamental missing type constrait on l specify it's trait. if that, should compiler do? when using new operator, type must known, here l abstract type new baselayer l entirely unknown @ point of call new. there no way compiler know here.

what should make layer abstract, , let concrete sub-classes perform instantation.

trait layer{   def write(s: string): string } trait timestamplayer extends layer{    abstract override def write(s: string) = system.nanotime + super.write(s) }  trait baselayer extends layer {   def write(s: string) = s } abstract class layertest {     type l <: layer    val layer: l // note abstract } class mytest extends layertest {   class l extends baselayer timestamplayer   val layer = new l // instantiate here in conrete test } 

note ease pain i've used fact can directly implement abstract type concrete class of same name (class l extends ...)


Comments