7

Per MDN

A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created.

Based on this statement is it acceptable to call methods in a class with access to global variables a type of closure?

It seems as though that description meets the requirements, as a method is of course a function for a class instance, and it has access to the class variables which are in a sense local to the method.

Aaron Hall
  • 6,003
tt9
  • 631

4 Answers4

6

Yes, but global variables have nothing to do with it.

A closure is a function that encloses state from an outer scope. This enclosed state is persistent, and more than one function can enclose the same set of state data.

An object, on the other hand, is a set of state variables with functions associated with it that (if encapsulation is being used) have privileged access to that state.

In other words, an object is a closure and a closure is an object. They're two different ways of looking at the same basic concept: state and associated functions bound together. The only difference is how you look at it. A closure emphasizes the functions a lot more, while an object tends to put more emphasis on the data, but the two are really the same thing. In fact, object-oriented languages almost always implement closures by having the compiler transform them into special object classes, and functional languages tend to implement objects by wrapping multiple closures around the same set of state data.

Mason Wheeler
  • 83,213
2

Are a class's methods a type of closure?

No. A closure is a very specific type of function. As Wikipedia says, they implement lexical scoping.

Closures

In Python, such a closure would look like this:

def startAt(x):
    def incrementBy(y):
        return x + y
    return incrementBy

The variable x is bound (in Python's case, immutably) in the incrementBy that is created when startAt is called. (x could very well be a mutable data structure, or the implementation of your language may allow you to change it.)

For contrast, here is a non-closure function example that uses a global:

x = 0
def incrementBy(y):
    return x + y

Here, there is a single function, incrementBy, and whenever x changes on the global scope, it is reflected in the function:

>>> incrementBy(5)
5
>>> x = 1
>>> incrementBy(5)
6

Methods

Methods, on the other hand, have access to mutable variables. If they are instance methods, they know their instance, and thus class. If they are class-methods, they know their class. Even if they are static methods, not knowing class or instance, they still may use globals. The globals may be reassigned at will, and can thus change the outputs of the function for any given.

You can make a closure into a method appended to a class, depending on the implementation of your language:

class Foo(object):
    ib5 = staticmethod(startAt(5))

>>> Foo.ib5(3)
8
>>> Foo.ib5
<function incrementBy at 0x7f29e978b758>

But I don't think that's what you actually mean to do.

What is a closure?

A closure is an inner function that binds variables to it from the outer function's scope. If a class's method doesn't do this, it's not a closure.

Comparing and Contrasting Methods and Closures

What they have in common, is combining functionality with data.

Where they contrast is closures bind data to functionality, whereas methods bind functionality to data structures. So it would not be correct to call a method a type of closure unless it really is a closure.

I would assert that most class's methods don't do this, but I'll allow that there may be environments where this is a recognized technique. I'd be very interested to know who is using this and what their justification is. I have personally never seen this being done in the wild.

Aaron Hall
  • 6,003
1

I think what you're asking is whether static methods and static variables of the same class can be considered as a (perhaps poor person's) closure construct.

While it has some resemblance to the operation of a closure, the construct you're describing cannot be created and recreated at will (unless you count launching of the program as a creation of the closure, which is a bit far fetched.)

Creation, and multiple concurrent instances, is a capability expected of closures -- in the text you're citing, for example, the article shows makeAdder, which is used to create two different closures that coexist.

The static class mechanism cannot be created or instantiated (it just exists primordially), so you would have trouble recreating the makeAdder example using it.

Erik Eidt
  • 34,819
0
  • A function closed within a function.
  • Closure is a function that can be stored as a variable with in a function.
  • The Closure can refer to any of the outer function's local variables.

Are a class's methods a type of closure?

class methods do not not satisfy the above basic principles, so simple methods with in the class are not closure.

for example anonymous classes in java are close to being called as a closure. SO Question

Origin: late Middle English: from Old French, from late Latin clausura, from claus- ‘closed’

Premraj
  • 916