Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exactly what is the difference between a "closure" and a "block"?

I've found that lots of people use the words closure and block interchangeably. Most of these people can't explain what they're talking about.

Some Java programmers (even ones from really expensive consultancies) talk about anonymous inner classes as "blocks" and "closures" - but I know this isn't true. (You can't pass mutable variables in from the scope of the method in which they're defined...)

I'm looking for:

  • a precise, computer science definition of a block
  • a precise, computer science definition of a closure
  • and clarification on the difference between the two.

I'd really like to see links, articles or book references on these please.

like image 321
Dafydd Rees Avatar asked Nov 28 '09 12:11

Dafydd Rees


People also ask

What is a closure?

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.

Why is it called closure?

A closure is a function and its scope assigned to (or used as) a variable. Thus, the name closure: the scope and the function is enclosed and used just like any other entity.

What is a closure in Rust?

Rust's closures are anonymous functions you can save in a variable or pass as arguments to other functions. You can create the closure in one place and then call the closure elsewhere to evaluate it in a different context. Unlike functions, closures can capture values from the scope in which they're defined.

What is difference between function and closure?

Difference between Function and ClosureFunction is declared using func keyword whereas Closure doesn't have func keyword. Function has always name but Closure doesn't have. Function doesn't have in keyword but closure has in the keyword.


1 Answers

While a block is just a piece of code that can be composed by statements and declarations but nothing else, a closure is a real first-class object, a real variable that has a block as its value.

The main difference is that a block simply groups instructions together (for example the body of a while statement), while a closure is a variable that contains some code that can be executed.

If you have a closure usually you can pass it as a parameter to functions, currify and decurrify it, and mainly call it!

Closure c = { println 'Hello!' } /* now you have an object that contains code */ c.call() 

Of course closures are more powerful, they are variables and can be used to define custom behaviour of objects (while usually you had to use interfaces or other OOP approaches in programming).

You can think of a closure as a function that contains what that function does inside itself.

Blocks are useful because they allow scoping of variables. Usually when you define a variable inside a scope you can override the outer definitions without any problems and new definitions will exist just during the execution of block.

for (int i = 0; i < 10; ++i) {      int t = i*2;      printf("%d\r\n", t); } 

t is defined inside the block (the body of the for statement) and will last just inside that block.

like image 158
Jack Avatar answered Sep 28 '22 09:09

Jack