Traits for "static" methods in Scala? -


are there cases it's preferable mixin traits access functionality of "static" methods, rather importing objects methods?

say want access functionality of method a(). ever extend trait contains a() rather import object contains a()?

if @ following example:

1)

trait {   def a() {} } 

...

class b extends {   val b = a() } 

vs.

2)

object {   def a() {} } 

...

import a._ class b {   val b = a() } 

is there reason prefer first approach, if there no "is-a" relationship between 2 classes b , a?

maybe things extend b don't want keep re-importing a?

maybe method relies upon other "static" methods want override implementation?

if b final (or object) , methods static (and don't refer implementations might want change in b), there's not point in mixing in trait. exception if there implicit conversions defined, if mix in implicit have lower priority if declare yourself.

(check out scala.lowpriorityimplicits mixed scala.predef examples.)


Comments