How should I pass an implicit parameter when I create a function object?
If you do not specify anything, you will get an error (in the part of val process=):
case class Cache (key:Int, value: String)
US>class Processor {
val process=processF_//could not find implicit value for parameter cache:this.Cache
private def processF() (implicit cache:Cache): String=cache.value
}
object Main {
def run() {
implicit val cache = Cache (10, "hello")
val processor = new processor
processor.process()
}
}
Main.run
I wish I could write it like this, but it doesn't work.
val process:()(implicit cache:Cache)=>String=processF_// syntax error
I don't know if it's possible in the first place, but could you tell me what kind of approach is effective?Thank you for your cooperation.
scala
I don't know if it matches the questioner's intentions, but how about passing the constructor argument to implicit
?
(For the sake of simplicity, we set the Main
object to App
.)
case class Cache (key:Int, value: String)
class Processor (implicit c: Cache) {
//implicit val cache2 = Cache(20, "hola")
val process=processF_
private def processF() (implicit cache:Cache): String=cache.value
}
object Main extensions App {
implicit val cache = Cache (10, "hello")
val processor = new processor
println(processor.process())
}
If implicit
precedes val
, like cache2
, they will go and see it.
582 PHP ssh2_scp_send fails to send files as intended
620 Uncaught (inpromise) Error on Electron: An object could not be cloned
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.