Hi,

I'm wondering about how to change a data structure without breaking the
API used to access it. For example, let's assume that I have a library
for dealing with records of people and I'm storing them in structs.

     (defstruct person :name)

The users of my library access the data stored in the records like any
other map.

     (:name some-person)

When the library's been in use for a while, I realize that I need to
make a change to the data structure.

     (defstruct :first-name :last-name)

The problem is that making this change would break all the clients using
the library.

The obvious and traditional solution, at least for someone used to
thinking in OO terms, would be to tell the clients of the library to
treat the records as opaque handles, and only use the library's accessor
functions to access their contents. However, I don't like this approach
because it leads to a lot of boilerplate code that's there "just in case
I'll need it some day". It also prevents my clients from using generic
map manipulation functions on the records.

In Python, my current go-to language, I can have my cake and eat it too
by using class properties.

     # A struct-like class whose members can be manipulated directly
     class Person1(object):
         name = "full name"

     p1 = Person1()
     print p1.name


     # Like above, but with three members, one of which is calculated by
     # a function when it is accessed
     class Person2(object):
         first_name = "first"
         last_name = "last"

         def get_name(self):
             return self.first_name + " " + self.last_name
         name = property(get_name)

     p2 = Person2()
     print p2.name   # The old API still works, even though the data is
                     # stored in a different way


What's the idiomatic Clojure way of dealing with this issue?


--
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