Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

How do you define a type for a function in Scala?

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])
question from:https://stackoverflow.com/questions/1853868/how-do-you-define-a-type-for-a-function-in-scala

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
trait Foo {
  type MyFunction = (Int,Int) => Boolean

  def checkInts(f: MyFunction)
  def checkInts(f: Option[MyFunction])
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...