scala - Implicit resolution from trait -


i have use case similar situation following:

trait {   implicit val x = "hello" }  class b {    // somehow bring x scope here???    def run(x: int)(implicit y: string) = y + x }  println((new b).run(3)) 

i understand need bring x defined in trait in implicit scope of b. i've tried following:

# attempt 1 # class b extends { .... } /// doesn't work  # attempt 2 # class b extends {    val x1 = implicitly[string]  /// doesn't work either    def run(x: int)(implicit y: string) = y + x } 

please explain missing here (or, point me relevant theory topic can study, new scala).

the value of 'implicit y' resolved in println-line not available. making variable implicitly available within body of class, resolution of implicit string not needed there.

implicit isn't magic; if can't reach implicit variable explicitly can't compiler.

what problem trying solve?


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -