How to define a function that takes a function literal (with an implicit parameter) as an argument?

前端 未结 2 1272
情书的邮戳
情书的邮戳 2021-01-14 05:45

I want to be able to do something on these lines (won\'t compile):

def logScope(logger:Logger)(operation: (implicit l:Logger) => Unit) {/* code */ operati         


        
2条回答
  •  有刺的猬
    2021-01-14 06:04

    In your second example try this:

    logScope(Logger()) { implicit logger =>
      operationOne
    }
    

    It should work fine. The logic here is that 'implicit' is an attribute of particular value inside closure, not a part of the closure's interface.

提交回复
热议问题