Factor Clojure code setting many different fields in a Java object

Posted by chris on Stack Overflow See other posts from Stack Overflow or by chris
Published on 2011-01-02T18:01:28Z Indexed on 2011/01/02 19:53 UTC
Read the original article Hit count: 253

Filed under:
|

How do I factor code setting many different fields in a Java object? I would like to factor

(set! (. employee name) "Chris")
(set! (. employee age) 100)
(set! (. employee salary) 5000)

to

(doseq [field '((name "Chris") (age 100) (salary 5000))]
  (set! (. employee (first field)) (second field)))

However this won't work because the period is a macro, and tries to evaluate (first field) literally. By the way, I understand that setting fields is not good practice. I need to inter-operate with legacy code.

© Stack Overflow or respective owner

Related posts about clojure

Related posts about field