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.

2
  • 1
    You are saying the methods have access to global variables, then saying they are local in a sense. Since you are not talking about any specific language, it's unclear what you mean. If they are really global, it really doesn't make sense to call it a closure.
    – psr
    Dec 29, 2015 at 0:42
  • 1
    You can consider class methods and instance methods partially applied functions instead. The pre-applied parameter is the instance (this) or a class. Unlike closures, methods do not capture the outer environment, though.
    – 9000
    Dec 29, 2015 at 2:59

4 Answers 4

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.

2
  • 4
    What does mutability have to do with closures? I have both seen closures with mutable data (you may just be confused by Pythons restriction on not letting you implicitly reassign a variable in an outer scope), and immutable objects. Note that it is possible to implement closures in terms of objects and vice versa. But the question seems to be specifically about class methods rather than instance methods, which your answer does not cover!
    – amon
    Dec 29, 2015 at 7:33
  • @amon points taken, answer updated, though I think the question: "is it acceptable to call methods in a class with access to global variables a type of closure?" doesn't specifically mean class methods. I think the questioner means any method in a class, be it an instance, class, or static method.
    – Aaron Hall
    Dec 29, 2015 at 13:53
5

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.

4
  • I don't agree to what you're claiming at all. An object is so different than a closure unless you're saying 'object' as an object of closure. When 'object' means just an instance object created by instantiating a class, it is a data structure, not closure at all. And a closure is another special form of 'object' where an object or objects are attached to a function. Jul 16, 2022 at 18:51
  • And I can't say anything for sure when it comes to functional programming as I lack knowledge. But if I may say anything out of my intuitions, I'd like to insist what you're saying doesn't apply to Haskell either. I conjecture that all the seemingly closures only exist in compile time and after being compiled, there are no closures. I think that's how static type system in Haskell is made use of and that's why programs written in Haskell are fast. Jul 16, 2022 at 19:11
  • @SmartHumanism "And a closure is another special form of 'object' where an object or objects are attached to a function." Yes, that's exactly the point I was making here: an object is data bound to one or more functions. And a closure is a function bound to a set of data, with the possibility of multiple closure functions sharing the same data. They're literally the exact same thing; the only real difference is which angle you're looking at it from, whether "the important part" is the data or the function(s). Jul 18, 2022 at 21:22
  • I agree with you, if you clearly states your view at the beginning. I think some beginners can be confused as their knowledge cannot be broad enough to assimilate your answer right away. Thank you for your reply. :) Jul 19, 2022 at 8:46
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.

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’

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.