简体   繁体   中英

Typed Function and Currying in Scala

In Scala let's say I have a function like this:

def foo[R](x: String, y: () => R): R

so I can do:

val some: Int = foo("bar", { () => 13 })

Is there a way to change this to use function currying without "losing" the type of the second argument?

def foo[R](x: String)(y: () => R): R
val bar = foo("bar") <-- this is now of type (() => Nothing)
val some: Int = bar(() => 13) <-- doesn't work

Functions can't have type parameters, you have to use a custom class like this:

def foo(x: String) = new {
  def apply[R](y: () => R): R = y()
}

val bar = foo("bar")
val some: Int = bar(() => 13)
// Int = 13

To avoid structural typing you could create custom class explicitly:

def foo(x: String) = new MyClass...

A variation on senia's answer, to avoid structural typing:

case class foo(x: String) extends AnyVal {
  def apply[R](y: () => R): R = y()
}

val bar = foo("bar")
val some: Int = bar(() => 13)
// Int = 13

Not really a solution to your problem but just to point out that you can still use the second version of your function if you supply the type explicitly:

scala> def foo[R](x: String)(y: () => R): R = y()
foo: [R](x: String)(y: () => R)R

scala> val bar = foo[Int]("bar") _
bar: (() => Int) => Int = <function1>

scala> bar(() => 12)
res1: Int = 12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM