Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference between closure and object.

193 views
Skip to first unread message

Jino, H.

unread,
Jun 1, 1999, 3:00:00 AM6/1/99
to
what is a difference between closure as a primitive object
and object in oop? It's confusing to me.
What do you think about that?


*'`'*.,.*'`'*.,.*'`'*.,.*'`'*.,.*'`'*.,.*
JinHo Hyun - Programming System Lab
Korea University
*'`'*.,.*'`'*.,.*'`'*.,.*'`'*.,.*'`'*.,.*
email : jhh...@psl.korea.ac.kr
home : http://oi.korea.ac.kr/~jhhyun
*'`'*.,.*'`'*.,.*'`'*.,.*'`'*.,.*'`'*.,.*

Jason Stokes

unread,
Jun 1, 1999, 3:00:00 AM6/1/99
to
On Tue, 01 Jun 1999 21:11:17 +0900, Jino, H. <jhh...@psl.korea.ac.kr> wrote:

> what is a difference between closure as a primitive object
> and object in oop? It's confusing to me.

A closure is an object -- specifically, a particular kind of function. I'm
not sure I understand the question beyond that, I'm afraid.

--
Jason Stokes: js...@bluedog.apana.org.au

pat...@c837917-a.potlnd1.or.home.com

unread,
Jun 1, 1999, 3:00:00 AM6/1/99
to
Jino, H. (jhh...@psl.korea.ac.kr) wrote:

: what is a difference between closure as a primitive object and

: object in oop? It's confusing to me. What do you think about
: that?

A closure is a function that has captured the local state in which it
is defined. In Scheme...

(define (make-adder x)
(lambda (y) (+ x y)))

(define add-one (make-adder 1))

(add-one 5) ;; This returns the number 6.

An object in OOP is a set of functions and state, so it is kind of
like a closure or a set of closures that all operate on the same
state. This kind of object can be created out of closures in Scheme...

(define (make-pair first second)
(lambda (message)
(if (equal? message 'first)
first
(if (equal? message 'second)
second))))

(define pair (make-pair 3 4))

(pair 'first) ;; This message returns the first value, 3.

(pair 'second) ;; This message returns the second value, 4.

--
Patrick D. Logan mailto:patric...@home.com

Simon Helsen

unread,
Jun 1, 1999, 3:00:00 AM6/1/99
to Jason Stokes
>> what is a difference between closure as a primitive object
>> and object in oop? It's confusing to me.
>
>A closure is an object -- specifically, a particular kind of function. I'm
>not sure I understand the question beyond that, I'm afraid.

I think the poster meant that closures and objects (as instances of
classes after Booch - just one way of understanding oop btw) are similar
in both power and implementation. Theoretically, they are quite different,
closures are living functions, whereas objects (again, in Booche's sense)
are living classes. But you can have more than one function within a
class, also ho-functions (although this is rarely found in OO - I only
know of Ocaml and Sisal as having them). Practically, closures are usually
objects on the heap, just as oo-objects (I use this funny due to the
unfortunate overloading of the word "object" -> another illustration
overloading is no good ;-). Looking at it that way, you may think of
closures as being special objects (in the oop-sense) and therefore you
might emulate first class functions using oop. This is, I think, what
Pizza originally did with Java (Pizza extended Java, a.o., with
first-class functions). But closure creation is usually cheaper. Also, you
don't have any overhead associated with the extra power of classes (more
than one function, inheritance, etc) If your language has records or
something similar as well, you can use ho-functions to program
"object-oriented". So you can achieve both with each other (well, of
course, if your oo- and ho-language are Turing-complete), but there are
efficiency-concerns and you should use both concepts for what they are
made, therefore, having both is very useful indeed.

hope this more or less answers the question...

Simon


Lynn Winebarger

unread,
Jun 1, 1999, 3:00:00 AM6/1/99
to
In article <3753CDE5...@psl.korea.ac.kr>,

Jino, H. <jhh...@psl.korea.ac.kr> wrote:
> what is a difference between closure as a primitive object
> and object in oop? It's confusing to me.
> What do you think about that?
>
An object is a closure with multiple entry and exit points. The main
difference is the ease of expressing the concepts. I can write higher
order functions in C, for example, it just takes some work. I could say
the same of OO languages implementing FP languages, but it's more
difficult to get the standard optimizations right.

Matthias Felleison has quite a lot to say about the similarity
between OOP and functional programming. He's at Rice's CS dept.

Lynn


0 new messages