我可以将任意函数传递给Scala中的另一个函数吗? [英] Can I pass an arbitrary function to another function in Scala?

查看:130
本文介绍了我可以将任意函数传递给Scala中的另一个函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Scala的新手,能够将函数传递给其他函数非常简洁 - 但我可以将任意函数引用传递给另一个函数吗?所述函数参数的参数将是固定的(也就是说,我也很好奇你是否可以传递任意函数的函数)。我一直因为类型错误而被绊倒。我已经尝试使用任何但它似乎没有帮助。



例如,我有下面的代码:

  class CodeRunner(val user_defined:(Int)=> Unit){
def run(input:Int )= {
user_defined(input)
}
}

def random_code(input:Int)= {println(Running with input+ input)}

val d1 =新的CodeRunner(任意代码)

d1.run(4)

我得到:

 输入4 

现在,假设我想传递以下函数:

  def random_code(input:String)= {println(使用输入运行+输入)} 

如何更改我的 CodeRunner 类来处理这两个问题?

解决方案

泛型类型允许您定义具有占位符类型的类,该对象类型在对象获得实例时得到指定特德。编译器很高兴,因为它可以确保所有内容都是类型安全的,并且您很高兴,因为您可以实例化对象并为该值传入任意类型。



为了在你的类中使用泛型类型,你可以像这样修改它:

  class CodeRunner [T](val user_defined :( T)=> Unit){
def run(input:T)= {
user_defined(input)
}
}

class CodeRunner之后的[T]是重要的部分 - 它定义了一个泛型类型T(可以用另一个资本替换T所以,如果你定义了一个方法:

pre> def random_code(input:String)= {println(使用输入运行+输入)}

,然后传入:

  val d1 =新的CodeRunner(任意代码)

...编译器然后说啊a,对于CodeRunner的这个实例,泛型类型T是一个字符串。如果您调用

  d1.run(string)

编译器会很高兴,但不会让你在d1.run(4)中传递。


I'm new to Scala, and being able to pass functions to other functions is pretty neat-- but can I pass an arbitrary function reference to another function? The arity of said functional parameter will be fixed (that said, I'm also curious about whether you can pass a function with arbitrary arity as well). I keep getting tripped up on type errors. I've tried using Any but it doesn't seem to help.

E.g., I have the code below:

class CodeRunner(val user_defined: (Int) => Unit) {
  def run(input: Int) = {
    user_defined(input)
  }
}

def arbitrary_code(input: Int) = { println("Running with input " + input) }

val d1 = new CodeRunner(arbitrary_code)

d1.run(4)

And I get:

Running with input 4

Now, let's say that I want to pass the following function instead:

def arbitrary_code(input: String) = { println("Running with input " + input) }

How can I change my CodeRunner class to handle both?

解决方案

Generic types allow you to define a class with a placeholder type that gets specified when an object gets instantiated. The compiler is happy because it can make sure that everything is type safe, and you're happy because you can instantiate the object and pass in arbitrary types for the value.

To use a generic type with your class, you could modify it like this:

class CodeRunner[T] (val user_defined: (T) => Unit) {
  def run(input: T) = {
    user_defined(input)
  }
}

The [T] after "class CodeRunner" is the important part -- it defines that there is a generic type T (you could replace T with another capital letter, etc.) which will be used within the class definition.

So, if you define a method:

def arbitrary_code(input: String) = { println("Running with input " + input) }

and then pass it in:

val d1 = new CodeRunner(arbitrary_code)

... the compiler then says "aha, for this instance of CodeRunner the generic type T is a string". And if you invoke

d1.run("string")

the compiler will be happy, but won't let you pass in d1.run(4).

这篇关于我可以将任意函数传递给Scala中的另一个函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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