Maybe I'm missing something, but what is wrong with Stuart Sierra's
solution?  I quite like it, and it would probably be more appealing if
it were encapsulated into a macro.

(def-propholder person)

(def me (person {:name "Matt Clark"}))

(def-propholder person2
  :name
    {:getter (fn [record] (str (:first-name record) " " (:last-name
record)))
     :setter (fn [record val] (throw (Exception. ":name cannot be set
any more")))})

(def me (person2 {:first-name "Matt" :last-name "Clark"}))

I'll be the first to admit my pseudo code is far from elegant, but it
wouldn't be that big of a challenge to write something like this that
fits your needs.  The even numbered argument indices following the
structure's name have the keys that have overrides, the odd numbered
contain the functions that do the overriding.  In the above example
the exception could be replaced with a split on the space in the full
name instead.
If I wanted to avoid macros however, I'd go with something like your
solution, I just don't like the trailing dashes for private data :(

Matt


On Apr 20, 8:52 am, Timo Mihaljov <noid....@gmail.com> wrote:
> 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