JinHo Hyun - Programming System Lab
email : jhh...@psl.korea.ac.kr
home : http://oi.korea.ac.kr/~jhhyun
> 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
: what is a difference between closure as a primitive object and
: object in oop? It's confusing to me. What do you think about
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)
(if (equal? message 'first)
(if (equal? message '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
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...
Matthias Felleison has quite a lot to say about the similarity
between OOP and functional programming. He's at Rice's CS dept.