简体   繁体   中英

understanding scala : currying

I recently started learning Scala and came across currying. From an answer in this post , this code snippet

def sum(a: Int)(b: Int) = a + b

expands out to this

def sum(a: Int): Int => Int = b => a + b

Then I saw a snippet from scala-lang , which shows it's possible to write something like this to emulate a while loop

  def whileLoop (cond : => Boolean) (body : => Unit) : Unit = {
      if (cond) {
          body
          whileLoop (cond) (body)
      }
  }

Out of curiosity, I tried to expand this out, and got this

  def whileLoop2 (cond : => Boolean) : (Unit => Unit) =
      (body : => Unit) =>
          if (cond) {
              body
              whileLoop2 (cond) (body)
          }

But there seems to be some syntax that I'm missing because I get an error

error: identifier expected but '=>' found.
(body : => Unit) => 
        ^

What is the proper way to expand out the emulated while loop?

The tricky part is dealing with the parameterless function or "thunk" type => Unit . Here is my version:

def whileLoop2 (cond: => Boolean): (=> Unit) => Unit =
  body =>
    if (cond) {
      body
      whileLoop2 (cond)(body)
    }

var i = 5
val c = whileLoop2(i > 0)
c { println(s"loop $i"); i -= 1 }

It appears that you can annotate the return type with (=> Unit) => Unit , but you cannot annotate (body: => Unit) , so you have to rely on the type inference here.

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