你如何为Scala中的函数定义类型? [英] How do you define a type for a function in Scala?
本文介绍了你如何为Scala中的函数定义类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,假设我需要一个函数,它需要两个Ints并返回一个布尔值,我可以定义一个函数,使用这样的:
def checkInts(f:(Int,Int)=> ;布尔)= {
//做东西
}
有没有如何定义f的类型?然后我可以这样做:
def checkInts(f:MyFunctionType)
或
def checkInts(f:Option [MyFunctionType ])
解决方案
特质Foo {
type MyFunction =(Int,Int)=>布尔
def checkInts(f:MyFunction)
def checkInts(f:Option [MyFunction])
}
I'm hoping there is a way to define a type for a function in Scala.
For example, say I want a function that takes two Ints and returns a Boolean, I could define a function that uses that like this:
def checkInts(f: (Int,Int) => Boolean) = {
// do stuff
}
Is there a way to define the type of f? Then I could do something like:
def checkInts(f: MyFunctionType)
or
def checkInts(f: Option[MyFunctionType])
解决方案
trait Foo {
type MyFunction = (Int,Int) => Boolean
def checkInts(f: MyFunction)
def checkInts(f: Option[MyFunction])
}
这篇关于你如何为Scala中的函数定义类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文