Discussion:
Closure similar to the Object in OOP?
(too old to reply)
q***@gmail.com
2012-06-11 19:19:25 UTC
Permalink
The closure is a function that has access to the locals, that is,
closure is the function with the local data, which has same meaning of Object with one function in Object-Oriented Programming.

Please correct me.
Torben Ægidius Mogensen
2012-06-12 12:45:28 UTC
Permalink
Post by q***@gmail.com
The closure is a function that has access to the locals, that is,
closure is the function with the local data, which has same meaning of Object with one function in Object-Oriented Programming.
Please correct me.
There is similarity between objects and closures, but where an object
usually contains mutable state and multiple methods that modify these,
closures usually contain immutable context and a single function that
can use this. There is also a difference in the way types are used.

Torben
George Neuner
2012-06-12 14:52:34 UTC
Permalink
Post by Torben Ægidius Mogensen
Post by q***@gmail.com
The closure is a function that has access to the locals, that is,
closure is the function with the local data, which has same
meaning of Object with one function in Object-Oriented
Programming.
Please correct me.
There is similarity between objects and closures, but where an object
usually contains mutable state and multiple methods that modify these,
closures usually contain immutable context and a single function that
can use this. There is also a difference in the way types are used.
Torben
Depends on the language. In most Lisp family languages all data is
mutable and so closures both contain mutable data and reference
mutable bindings.

I don't want to start an argument here about whether Lisp is a FP
language ... let's just say Lisp is multi-paradigm and easily supports
functional programming.

George
Andrew Reilly
2012-06-14 11:58:51 UTC
Permalink
Post by Torben Ægidius Mogensen
Post by q***@gmail.com
The closure is a function that has access to the locals, that is,
closure is the function with the local data, which has same meaning of
Object with one function in Object-Oriented Programming.
Please correct me.
There is similarity between objects and closures, but where an object
usually contains mutable state and multiple methods that modify these,
closures usually contain immutable context and a single function that
can use this. There is also a difference in the way types are used.
Where is the context immutable? All of the examples I've seen (and used)
of closures in various languages used mutable state as the essential
reason for doing it. Also: there are plenty of scheme libraries that
implement various object schemes on top of closures, so it is clearly
possible. Simple matter of macrology, if you want nice syntax for
"methods", even simpler if you're prepared to muck about at run-time and
use a "prototype" object model, rather than a class-based one.

Cheers,
--
Andrew Reilly
Dirk Thierbach
2012-06-14 21:14:13 UTC
Permalink
Post by Andrew Reilly
Post by Torben Ægidius Mogensen
There is similarity between objects and closures, but where an object
usually contains mutable state and multiple methods that modify these,
closures usually contain immutable context and a single function that
can use this. There is also a difference in the way types are used.
Where is the context immutable? All of the examples I've seen (and used)
of closures in various languages used mutable state as the essential
reason for doing it.
And all the examples where I've used closures don't use mutable
state. :-)
Mostly because I like to use languages the allow partial applications
and make a strict disctinction between mutable and immutable state.

So I would second Torben: Conceptually, one uses closures to capture
context, and one uses objects to group mutable state together with
functions that ensure certain invariants on the mutable state.

In a language that has mutable state and closures, one can use that to
emulate objects, but it's still a different concept. Tough using one
concept (or a set of concepts) to emulate a different concept is of course
nothing new in a Turing-complete language.

- Dirk

George Neuner
2012-06-12 15:09:08 UTC
Permalink
Post by q***@gmail.com
The closure is a function that has access to the locals, that is,
closure is the function with the local data, which has same
meaning of Object with one function in Object-Oriented
Programming.
As Torben has said, there is a lot of similarity.

Compared to class based OO, closures are a more general concept
because they can, at run time, bind arbitrary functions to arbitrary
data without the need for prior declarations. Several early Lisp
object systems were based on this ability.

Using closures AS objects is similar to so-called "template" or
"prototype" OO, in which there is no real distinction between class
and instance: object data structure and member function associations
can be changed at run time, and objects are created by "cloning" a
representative object. (See Self, Lua, Javascript, etc.)

George
Loading...