Scroll down for reply:

On Jan 21, 8:07 pm, Ashley Moran <[email protected]>
wrote:
> On 21 Jan 2011, at 17:28, Peter Morris wrote:
>
> > 1) Polymorphism appears to be handled by the ability to supply different 
> > implementations of a keyword dependent on parameters.
> > This means your oo methods are broken up out of your class definitions and 
> > sprinkled around your keyword definitions.
>
> I've flicked ahead a bit to chapter 3 of SICP and they describe message 
> passing in Scheme as implemented like this:
>
>   (define (make-account balance)
>     (define (withdraw amount)
>       (if (>= balance amount)
>         (begin
>           (set! balance (- balance amount))
>           balance)
>           "Insufficient funds!"))
>     (define (deposit amount)
>       (set! balance (+ balance amount))
>       balance)
>     (define (dispatch m)
>       (cond ((eq? m 'withdraw) withdraw)
>             ((eq? m 'deposit) deposit)
>             (else
>               (error "Unknown request -- MAKE-ACCOUNT")
>               m)))
>      dispatch)
>
>   (define acc (make-account 100))
>
>   ((acc 'withdraw) 40) ;=> 60
>
> To the best of my understanding, an "object' is a function with mutable 
> state.  I think it's a bit like a static variable in a C function, except 
> bound to the instance of the function.

Well objects are not really a well defined term.  However I'd think of
an object being data + behaviour (methods), where both the data and
behaviours move around together.  I'm not that familiar with Scheme
(and want to read the wizard book again), but my understanding of what
is happening above is that the 'objects' are created out of functions
sharing closures.  The same is possible in Clojure, and any language
with Closures, e.g, here two functions share a closure over an atom
holding an integer, this is a strong form of encapsulation.

(defn create-obj []
  (let [i (atom 0)]
    {:add-one (fn [] (swap! i inc))
     :sub-one (fn [] (swap! i dec))}))

(def a (create-obj))

((a :add-one)) ; => 1
((a :add-one)) ; => 2
((a :sub-one)) ; => 1

> I'd like to know if Clojure uses a similar convention for OO or if it does 
> something different.

The code above is obviously workable, and occasionally you might see
things like that, but Clojure isn't an OO language and doesn't really
want to be.  Instead Clojure lets you choose the features of OO
independently, allowing you to pick'n'mix as you please:

- Polymorphism
- Identity
- State
- Namespaces
- Hierarchy

Rich Hickey argues that conflating all these notions inside one (or
two) language construct (the object) (and the class) often has
undesirable consequences, and that they're more useful independently.
Having used Clojure, I'd agree :-)

> I'm looking forward to chapter 3 of SICP as comparing the Scheme and Ruby 
> approaches to OO should be interesting.

Yes, SICP is an enlightening book.

R.

-- 
You received this message because you are subscribed to the Google Groups 
"NWRUG" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nwrug-members?hl=en.

Reply via email to