Laurent PETIT wrote:
> What do others think about these 2 above statements ?
> 
>     The standard OO approach to information hiding would be private fields
>     and accessor methods. Any suggestions for the One True Clojure Pattern
>     that addresses the same problem?
> 
> 
> I think accessor methods.

Based on our discussion so far, I would use the following approach. In
the spirit of defn-, I appended a dash after field names that should be
accessed via accessor functions. This is similar to the Python idiom of
prefixing "private" members with an underscore to prevent _accidental_
direct modification. The symbols are also used as the initial versions
of the getter functions. This way there's so little code that no macros
are needed.

     ;;; Initial version

     ;; Library
     (defstruct person :full-name-)

     (defn new-person [full-name]
       (struct person full-name))

     (def full-name :full-name-)

     ;; Client
     (def p (new-person "James Bond"))
     (println (full-name p))


     ;;; Modified version

     ;; Library
     (defstruct person :first-name- :last-name-)

     (defn new-person [full-name]
       (let [[first-name last-name] (.split full-name " ")]
         (struct person first-name last-name)))

     (def first-name :first-name-)
     (def last-name :last-name-)

     (defn full-name [the-person]
       (str (first-name the-person) " " (last-name the-person)))

     ;; Client
     (def p (new-person "James Bond"))
     (println (last-name p))
     (println (full-name p))

How does this code look like to a experienced Clojure programmer? Does
it seem to be in the spirit of the language?

--
Timo

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to