Atif
Atif

Reputation: 424

Can we have closures without inner functions?

A closure is an outer function O which in its body have a variable x and an inner function F. F must access x. O must return F, i.e. the F itself (or its pointer), not the return value of F. This way since F can be called from code outside O, and F need x to be executed, x have to be kept in scope after O exit.

In javascript we can define functions within functions so its straight forward. In C# we have to use anonymous methods or anonymous delegates to have same behavior as inner functions.

The essense of a closure is preserving the values of local variables of a function, after the function exits, so that next time the function is called, the previous values are already present. The above mentioned syntax and techniques are ways to accomplish closures.

Is there anyway, in any language (other than assembly language) to have the essence of closures without using an inner function or delegate. Ofcourse fields in classes in OOP have same effect, but i am looking for ways other than that.

Upvotes: 1

Views: 182

Answers (2)

Mark Hurd
Mark Hurd

Reputation: 10931

I wouldn't call them closures, but the concept of local variables retaining state is in VB with Static local variables.

Sub Example()
  Dim normalVar As Integer
  Static staticVar As Integer

  normalVar += 1
  staticVar += 1

  Console.WriteLine("{0} {1}", normalVar, staticVar)
End Sub

(This is VB.NET but Static was in VB3 or earlier.)

This will output

1 1
1 2
1 3

if called three times.

Upvotes: 0

newacct
newacct

Reputation: 122489

A closure, by definition, closes over variables in its lexical scope. It would be utterly useless if it could not be "inner" to something else with local variables. So pretty much a closure must be capable of being inside some function.

However, one complexity is that closures in some languages are closely associated with anonymous functions. (Anonymous functions generally must be closures; closures need not be anonymous functions, but in some languages the only closures are anonymous functions.) And the syntax for anonymous functions may be distinct from the syntax for regular (named) functions (especially in languages with a separate namespace for functions and variables; named functions would be in the function namespace, but anonymous functions would be data). And in some of these languages, there are no "inner" named functions.

For example, in PHP, all named functions are in the global scope. Putting a named function definition inside another (counterintuitively) defines the inner function in the global scope (outside code can see it) when the outer function is run (as if it were declared in the outer scope, but delayed). However, PHP 5.3+ also has anonymous functions which are closures; people generally refer to the anonymous functions as "closures" because those are the only closures in the language.

Another example is Ruby. The def syntax defines a method. Defining a method inside another method (counterintuitively) still defines the method as visible to other methods. Ruby also has anonymous functions which are closures, using either the lambda, proc, or Proc.new syntax, but those are syntactically different.

Upvotes: 2

Related Questions