something like this came up on irc the other day. this is a good opportunity for someone to write some macros that allow you to specify a validator function for structs similar to refs and agents.
On Sat, Mar 28, 2009 at 2:20 PM, mikel <mev...@mac.com> wrote: > > > > On Mar 28, 4:05 pm, billh04 <h...@tulane.edu> wrote: >> I am using keywords as constants in my clojure programs, but I am >> worried about misspelling them. For example, I am writing a game and I >> am using the keywords :north and :south to indicate objects that >> belong to the north player or the south player. With this, I can >> define how to create the pieces for the players: >> >> (defn createPiece [id player] {:tag ::piece, :id id, :player >> player}) >> >> where player can take the values :north or :south. I have many other >> such constants in the program. >> >> I have been thinking of doing the following to catch misspellings: >> >> (def -north- :north) >> (def -south- :south) >> >> and then using these two definitions as follows: >> >> (def northPiece1 (createPiece 1 -north-)) >> >> rather than doing this: >> >> (def northPiece1 (createPiece 1 :north)) >> >> But, before I make these changes, I wondered if anyone else has run >> into this problem, and if it is worth it to consider an option to be >> able to declare the known constant keywords that will be used in the >> program and then treat the undeclared keywords as warnings? >> >> Another option would to be able to get a cross-reference of all >> keywords in the clj files within a folder (and subfolders) that shows >> where they are declared and used (like was done for Cobol programs in >> the 1970's). > > Another option you might consider is defining functions that sanity- > check your keys and values, so you can say, for example > > (defn createPiece [id player] > (with-allowed-keys > [:player :tag :id] > (with-allowed-values > {:player [:north :south]} > {:tag ::piece, > :id id, > :player player}))) > > > > > -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---