Re: Help with the dot operator special form
On Mar 21, 10:23 pm, Timothy Pratley timothyprat...@gmail.com wrote: You may be able to achieve what you want by directly accessing Clojure's reflector class instead of using the special form: You could also call Java's reflection API directly. -Stuart Sierra --~--~-~--~~~---~--~~ 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: Help with the dot operator special form
Thanks all for the pointers, this looks like a workable approach. In my case I'm not bothered by the performance hit from reflection (CPS transformation creates an obscene number of anonymous functions anyway). However I am running into an issue. Here's my dot function: (def not-seq? (comp not seq?)) (defn dot [obj member-expr] (let [member (str (first member-expr)) arg-or-args(rest member-expr) args (if (not-seq? arg-or-args) [arg-or-args] arg-or-args)] (Reflector/invokeInstanceMethod obj member (to-array args This works fine for: (dot Hello (list 'substring 1 2)) But throws an exception for this: (let [myref (ref {})] (dot clojure.lang.LockingTransaction (list 'runInTransaction (fn [] (commute myref assoc :mykey :myval) I'm getting a instance method not found exception which seems odd. I looked at LockingTransaction.java and I see that runInTransaction does in fact take Callable, and fn's are Callable. Any thoughts? I knew I would have to really learn Java at some point ;) On Sun, Mar 22, 2009 at 12:06 PM, Stuart Sierra the.stuart.sie...@gmail.com wrote: On Mar 21, 10:23 pm, Timothy Pratley timothyprat...@gmail.com wrote: You may be able to achieve what you want by directly accessing Clojure's reflector class instead of using the special form: You could also call Java's reflection API directly. -Stuart Sierra --~--~-~--~~~---~--~~ 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: Help with the dot operator special form
(let [myref (ref {})] (dot clojure.lang.LockingTransaction (list 'runInTransaction (fn [] (commute myref assoc :mykey :myval) I'm getting a instance method not found exception which seems odd. I looked at LockingTransaction.java and I see that runInTransaction does in fact take Callable, and fn's are Callable. Any thoughts? I haven't double checked the clojure code, but it looks like you are trying to call a static method, not an instance method, and that is what is causing the exception. --Eric Tschetter --~--~-~--~~~---~--~~ 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: Help with the dot operator special form
That was it! At one point I knew these things. Thanks much. On Sun, Mar 22, 2009 at 2:18 PM, Eric Tschetter eched...@gmail.com wrote: (let [myref (ref {})] (dot clojure.lang.LockingTransaction (list 'runInTransaction (fn [] (commute myref assoc :mykey :myval) I'm getting a instance method not found exception which seems odd. I looked at LockingTransaction.java and I see that runInTransaction does in fact take Callable, and fn's are Callable. Any thoughts? I haven't double checked the clojure code, but it looks like you are trying to call a static method, not an instance method, and that is what is causing the exception. --Eric Tschetter --~--~-~--~~~---~--~~ 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: Help with the dot operator special form
Thanks again to all for the help, clj-cont now supports the new and dot special forms. This also means that dosync, doto, .. all work perfectly fine from within a with-call-cc form. You can now write things like this: (let [cc (atom nil)] [(with-call-cc (. (let-cc k (reset! cc k) (k Hello)) substring 2)) (@cc Goodbye)]) - [llo odbye] One caveat is that you can't use the let-cc form within a dosync block that's embedded in a with-call-cc. This is probably for the best anyway. Also since the dot and new forms are being transformed into a version that relies on reflection you can't expect this code to be super performant. That may or may matter depending on your use case ;) On Sun, Mar 22, 2009 at 5:28 PM, David Nolen dnolen.li...@gmail.com wrote: That was it! At one point I knew these things. Thanks much. On Sun, Mar 22, 2009 at 2:18 PM, Eric Tschetter eched...@gmail.comwrote: (let [myref (ref {})] (dot clojure.lang.LockingTransaction (list 'runInTransaction (fn [] (commute myref assoc :mykey :myval) I'm getting a instance method not found exception which seems odd. I looked at LockingTransaction.java and I see that runInTransaction does in fact take Callable, and fn's are Callable. Any thoughts? I haven't double checked the clojure code, but it looks like you are trying to call a static method, not an instance method, and that is what is causing the exception. --Eric Tschetter --~--~-~--~~~---~--~~ 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: Help with the dot operator special form
you want defmacro not definline. the result of a macro is a data structure. that data structure is then evaluated in place of the call to the macro. definline (I think?) behaves similar to a function, so if it returns a data structure, you just get that data structure (the data structure is not then evaluated) On Sat, Mar 21, 2009 at 6:04 PM, David Nolen dnolen.li...@gmail.com wrote: I'm wondering if it's possible to create a Clojure function that does what the dot operator does. It seems like this would be possible with definline but I'm unable to get this to work or figure it out. For example I want to be able write something like the following: (dot Hello world (list 'substring 1 2)) Trying to use definline like this: (definline dot [obj member-exp] `(. ~obj (~...@member-expr))) Simply throws an error. I don't need variable arity, I will always pass an instance or class following by a list representing the member expression. Is this impossible? It seems like this would be generally useful to allow variable method calling on Java objects. As to why I want it to implement this, it would be far simpler to support Java interop from clj-cont if the dot operator could be expressed as a Clojure function. Thanks! David -- 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 -~--~~~~--~~--~--~---
Re: Help with the dot operator special form
On Sat, Mar 21, 2009 at 9:10 PM, Kevin Downey redc...@gmail.com wrote: you want defmacro not definline. the result of a macro is a data structure. that data structure is then evaluated in place of the call to the macro. definline (I think?) behaves similar to a function, so if it returns a data structure, you just get that data structure (the data structure is not then evaluated) You missed the part that it needs to be function not a macro. --~--~-~--~~~---~--~~ 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: Help with the dot operator special form
Or rather I did not express that requirement clearly enough. On Sat, Mar 21, 2009 at 9:21 PM, David Nolen dnolen.li...@gmail.com wrote: On Sat, Mar 21, 2009 at 9:10 PM, Kevin Downey redc...@gmail.com wrote: you want defmacro not definline. the result of a macro is a data structure. that data structure is then evaluated in place of the call to the macro. definline (I think?) behaves similar to a function, so if it returns a data structure, you just get that data structure (the data structure is not then evaluated) You missed the part that it needs to be function not a macro. --~--~-~--~~~---~--~~ 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: Help with the dot operator special form
You may be able to achieve what you want by directly accessing Clojure's reflector class instead of using the special form: user= (clojure.lang.Reflector/invokeInstanceMethod Hello substring (to-array [1 2])) e There is also invokeStaticMethod (and others). Regards, Tim. On Mar 22, 12:04 pm, David Nolen dnolen.li...@gmail.com wrote: I'm wondering if it's possible to create a Clojure function that does what the dot operator does. It seems like this would be possible with definline but I'm unable to get this to work or figure it out. For example I want to be able write something like the following: (dot Hello world (list 'substring 1 2)) Trying to use definline like this: (definline dot [obj member-exp] `(. ~obj (~...@member-expr))) Simply throws an error. I don't need variable arity, I will always pass an instance or class following by a list representing the member expression. Is this impossible? It seems like this would be generally useful to allow variable method calling on Java objects. As to why I want it to implement this, it would be far simpler to support Java interop from clj-cont if the dot operator could be expressed as a Clojure function. Thanks! David --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---