Re: Closures versus objects
Pascal Costanza <[email protected]>
| Newsgroups | gmane.comp.lang.lightweight |
|---|---|
| Message-ID | <[email protected]> |
On 17 Feb 2007, at 21:17, Mike Newhall wrote:
> At 02:49 AM 2/17/2007 -0800, Steve Dekorte wrote:
>> Now we can do dynamic binding with functions:
>>
>> Object_add_method(Collection, Collection_move_first_to_last,
>> function
>> (collection)
>> { Object_send(collection, Collection_at_insert, 0, Object_send
>> (collection, Collection_pop_last)) }
>>
>> Now we get the benefits of dynamic binding, but at the cost of (at
>> least) mixed semantics.
>>
>> - Steve
>
> They might be separate issues. For example, in the presence of
> generic functions, which are dispatched dynamically on type and do
> not introduce any new syntax at the call site, one can have the
> best of both worlds and do things like:
>
> move_first_to_last = function(seq) { atInsert(seq, 0, popLast
> (seq)) }
>
> 'seq' just needs to be a type for which the two generics used are
> implemented, the same requirement as in using methods.
It's a pity that there is not enough material "out there" that
explains generic functions in simple terms.
In the recent years, I have used a simplified version of CLOS in
courses and presentations that seems to work quite well in showing
the principle behind generic function dispatch. I have just set up a
website at http://p-cos.net/lisp/MiniCLOS/ that contains the code
both for Scheme (R5RS) and for a subset of Common Lisp. It doesn't
contain any comments at the moment, so might not be as easy to
follow, but I hope I will find some time to add the necessary
comments. Anyway, maybe this already helps to see how OOP based on
generic functions fits better with functional programming.
(I have recently become aware of work done by Giuseppe Castagna on
formalizing generic functions, which might be interesting for some.
See http://www.di.ens.fr/~castagna/book.html and http://
www.pps.jussieu.fr/~gc/papers/infcomp95.pdf )
Pascal
--
Pascal Costanza, mailto:[email protected], http://p-cos.net
Vrije Universiteit Brussel, Programming Technology Lab
Pleinlaan 2, B-1050 Brussel, Belgium