我如何从Scala中的方法获取Function对象? [英] How can I obtain Function objects from methods in Scala?

查看:134
本文介绍了我如何从Scala中的方法获取Function对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在Scala中有一个简单的类:

pre $ class $ Simple $ {
def doit(a:String) :Int = 42
}

如何存储一个val Function2 [Simple,字符串,Int],它需要两个参数(目标Simple对象,String参数),并且可以调用doit()让我返回结果? / div>

  val f:Function2 [Simple,String,Int] = _.doit(_)


Suppose I have a simple class in Scala:

class Simple {
  def doit(a: String): Int = 42
}

How can I store in a val the Function2[Simple, String, Int] that takes two arguments (the target Simple object, the String argument), and can call doit() get me the result back?

解决方案

val f: Function2[Simple, String, Int] = _.doit(_)

这篇关于我如何从Scala中的方法获取Function对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆