scala: override implicit parameter to constructor -


i have class takes implicit parameter used functions called inside class methods. want able either override implicit parameter, or alternatively, have implicit argument copied source. example:

def somemethod()(implicit p: list[int]) {   // uses p }  class a()(implicit x: list[int]) {    implicit val other = list(3) // doesn't compile    def go() { // don't want put implicit inside here since subclasses override go() have duplicate     somemethod()   } } 

the behavior want somemethod() gets implicit parameter changed version of x, class's implicit parameter. want able either mutate x without changing whatever passed a's constructor, or otherwise override new value of choosing. both approaches don't seem work. is, doesn't copy list in former case, , compiler finds ambiguous implicit value latter case. there way this?

i realize can redefine implicit value within go(), not choice in case because class subclassed numerous times, , i'd handle implicit change in base class only. doesn't need go in constructor, must in method other go().

introduce wrapper type, disambiguate:

//  badly named, choose domain-specific case class listholder(thelist: list[int])  def somemethod()(implicit holder: listholder) {   val xs = holder.thelist   // uses xs ... }  class a()(implicit xs: list[int]) {    implicit val other = listholder(42 :: xs) // compiles    def go() {     // xs never considered implicit param somemethod()     // because it's wrong type   } } 

this makes code more self-documenting, becomes blindingly obvious 2 implicits not 1 , same.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -