Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Scala have an equivalent to golangs defer?

Tags:

go

scala

Does Scala have an equivelent to golangs defer?

from: http://golang.org/doc/effective_go.html#defer

Go's defer statement schedules a function call (the deferred function) to be run immediately before the function executing the defer returns. It's an unusual but effective way to deal with situations such as resources that must be released regardless of which path a function takes to return. The canonical examples are unlocking a mutex or closing a file.

like image 359
Ryan Leach Avatar asked Sep 17 '13 08:09

Ryan Leach


People also ask

What is the point of defer Golang?

In Golang, the defer keyword is used to delay the execution of a function or a statement until the nearby function returns. In simple words, defer will move the execution of the statement to the very end inside a function.

Is there defer in Java?

The defer keyword postpones the execution of a function or statement until the end of the calling function. It executes code (a function or expression) when the enclosing function returns before the closing curly brace }. It is also executed if an error occurs during the execution of the enclosing function.

Can defer change return value?

defer can refer to and change the return values.

How do you defer in Python?

Defer pushes a function call to a stack. When the function containing the defer statement returns, the defered function calls are popped and executed one by one, in the scope that the defer statement was inside in the first place. Defer statements look like function calls, but are not executed until they are popped.


2 Answers

Scala does not offer defer by design, however you can create it yourself by wrapping your function in another function, passing an object which keeps track of functions to call.

Example:

class DeferTracker() {
  class LazyVal[A](val value:() => A)

  private var l = List[LazyVal[Any]]()
  def apply(f: => Any) = { l = new LazyVal(() => f) :: l }
  def makeCalls() = l.foreach { x => x.value() }
}

def Deferrable[A](context: DeferTracker => A) = {
  val dt = new DeferTracker()
  val res = context(dt)
  dt.makeCalls
  res
}

In this example, Deferable would be the wrapping function which calls context and returns it contents, giving it an object which tracks defer calls.

You can use this construct like this:

def dtest(x:Int) = println("dtest: " + x)

def someFunction(x:Int):Int = Deferrable { defer =>
  defer(dtest(x))
  println("before return")
  defer(dtest(2*x))

  x * 3
}

println(someFunction(3))

The output would be:

before return
dtest: 6
dtest: 3
3

I'm aware that this can be solved differently but it is really just an example that Scala supports the concept of defer without too much fuss.

like image 72
nemo Avatar answered Oct 15 '22 23:10

nemo


I can't think of a Scala specific way, but wouldn't this be equivalent (though not as pretty):

try {
    // Do stuff   
} finally {
    // "defer"
}
like image 26
fresskoma Avatar answered Oct 15 '22 22:10

fresskoma