On Thu, Nov 20, 2008 at 3:23 PM, islon <[EMAIL PROTECTED]> wrote:
>
> in clojure I have to do (player is a struct and its attributes are
> refs):
>
> (def player (Player ...))
>
> (dosync (ref-set player (assoc player :str (inc (:str player)))))

Assuming you meant:
(def player (ref {:str 55}))
(dosync (ref-set player (assoc @player :str (inc (:str @player)))))

This could be written:
(dosync (commute player update-in [:str] inc))

This is somewhat shorter, but also handles more deeply-nested structures easily.

> In scala when I want mutability I just define the variable with "var"
> instead of "val", just one letter, simple and elegant.

Simple, perhaps, but very much prone to the kind of threading issues
that Clojure is designed to solve.  You may say today that the game is
single-threaded, but if you ever change your mind Clojure will have
helped you structure your code well already -- in Java or Scala you'll
be in for some trouble.

Work to reduce how often you modify the state, using more functional
style until you have exactly the player object you want, and then do a
single commute or alter.  You should find you don't need to alter
state in as many places as you would in other languages, and when you
do it's easier to do it in a thread-safe way.

--Chouser

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to