scala implicit method with multiple arguments

落爺英雄遲暮 提交于 2019-12-10 13:10:03

问题


In a comment for SIP-13 Martin Odersky implied that it is possible to create an implicit method with multiple arguments. According to my experiences, implicit methods always have exactly one argument and I cannot imagine how an implicit method with multiple arguments can be used. Can someone give some use case and explanation?


回答1:


For example if you need an implicit parameter of a function type:

implicit def foo(x: Int, y: Int) = y * x

def bar(x: Int, y: Int)(implicit f: (Int, Int) => Int) = f(x,y)

scala> bar(3,4)
res3: Int = 12


来源:https://stackoverflow.com/questions/10935232/scala-implicit-method-with-multiple-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!