Thinking in Clojure - static fields?
I'm new to Clojure and I'm wondering how I'm supposed to be thinking about various things. I want to use the c3p0 database connection pool in a Clojure app. In Java, I would simply create an instance of this class and either assign it to a static field or perhaps make it available in a singleton and use it from there. In Clojure, am I supposed to use a var or a ref for this? Or something else entirely? Many thanks - phil. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Thinking in Clojure - static fields?
I'm new to Clojure and I'm wondering how I'm supposed to be thinking about various things. I want to use the c3p0 database connection pool in a Clojure app. In Java, I would simply create an instance of this class and either assign it to a static field or perhaps make it available in a singleton and use it from there. In Clojure, am I supposed to use a var or a ref for this? Or something else entirely? Many thanks - phil. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Thinking in Clojure - static fields?
Here is one way: (let [dbc (make-db)] (defn get-apples [] (.query dbc "select * from apples"))) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Thinking in Clojure - static fields?
On Mon, Jan 26, 2009 at 11:13 PM, Whirlycott wrote: > > I'm new to Clojure and I'm wondering how I'm supposed to be thinking > about various things. I want to use the c3p0 database connection pool > in a Clojure app. In Java, I would simply create an instance of this > class and either assign it to a static field or perhaps make it > available in a singleton and use it from there. > > In Clojure, am I supposed to use a var or a ref for this? Or > something else entirely? Refs are only useful if you plan to change the value of the ref to point to a new object. It sounds to me like you don't plan to do this. On the other hand, Vars are created most often via 'defn', and generally their values never change and they are globally accessible. I think a Var would do very nicely in this case. (def connection-pool (new DatabasePool ...)) --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 clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---