#clojure logs

2008-09-06

08:51achim_p_hi!
08:52jgracinhi, achim_p_
08:53achim_p_i tried using reflection with clojure, like this:
08:53achim_p_(.getMethods java.lang.String) ; (1)
08:53achim_p_(.getMethods (class "")) ; (2)
08:54achim_p_(1) throws a CompilerException, but (1) works ... anybody got an idea why?
08:55achim_p_(class java.lang.String) and (class (class "")) are both java.lang.Class
08:58achim_p_sorry, (2) works, (1) does not
08:59jgracinachim_p_, java.lang.String evals to a different thing than (class java.lang.String).
09:00achim_p_yes, but j.l.String is an instance of j.l.Class, so i should be able to call getMethods on it, shouldn't i?
09:02achim_p_i don't know much about java ...
09:05jgracinachim_p_, java.lang.String is not an instance of j.l.Class.
09:05jgracinString.class is
09:06jgracinI think the right thing to use is (.getMethods (class java.lang.String))
09:09achim_p_jgracin: hmm, strange. this actually returns j.l.Class's methods
09:09achim_p_(into [] (.getMethods (class java.lang.String)))
09:10achim_p_user> (instance? java.lang.Class java.lang.String)
09:10achim_p_true
09:11achim_p_i'm confused ;-)
09:11jgracinachim_p_, oh, sorry, I obviously don't know what I'm talking about. Let me try to figure it out... :-)
09:21jgracinrhickey, Hi!. What does java.lang.String symbol evaluate to? achim_p_ asked why (.getMethods java.lang.String) does not work and I'm confused.
09:23lisppaste8achim_p pasted "reflection" at http://paste.lisp.org/display/66398
09:26rhickeyString evaluates the the Class instance for String, but . is a special operator and doesn't evaluate its first arg if it looks like a classname. (.getMethods String) is sugar for (. String getMethods) which won't work because, given a class name, it considers the call a static method of the class, so things like (. String valueOf 42) work. But getMethods isn't a static method of String, it's an instance method of Class
09:26rhickey(.getMethods (identity String)) will work
09:29achim_p_rhickey: ah, that makes sense, thanks for the explanation!
09:29rhickeyIf you can't say X.y() in Java you can't say (. X y) in Clojure, and you can't say String.getMethods() in Java
10:37parth_mHello. I have a map (or hash-map) in Clojure and need to pass it to a java method that takes java.util.HashMap. Whats the idiomatic way to do this?
10:38parth_mDo I write a simple conversion function using doto?
10:39rhickey(reduce (fn [m [k v]] (do (.put m k v) m)) (java.util.HashMap.) {:a 1 :b 2 :c 3})
10:40rhickeyoops, no do needed:
10:40rhickey(reduce (fn [m [k v]] (.put m k v) m) (java.util.HashMap.) {:a 1 :b 2 :c 3})
10:41parth_mExactly what I was looking for. Thanks :-)
16:30ppjtGreetings, everyone. I've having an issue accessing a Java nested class. I've imported net.sf.saxon.s9api.Serializer -- which maps to the physical directory-class tree -- but can't get go net.sf.saxon.s9api.Serializer.Property -- the last of which is a nested class inside Serializer.
16:30ppjtAnyone else encountered something like this?
16:32blackdogyou get nested classes with s9api$Serializer i think
16:32rhickey_ppjt: the JVM syntax for a nested class is EnclosingClass$Nested class, so Serializer$Property
16:35ppjtOkay, thanks.
16:37ppjtOkay, just to confirm: Serializer has been imported, but clojure says it can't resolve the symbol Serializer$Property. Am I doing something obvious wrong?
16:41rhickey_ppjt: you need to import Serializer$Property too
16:43ppjtrhickey_: Ah, got it.
16:43ppjtThanks for the remedial Java help.
16:43ppjtClojure's been my only real motivation for hopping on the platform...
16:43rhickey_sure, the $ is hidden by Java except in the .class file names
16:44ppjtI saw those in the .class files, but thought it looked too machine-generated to type...
16:45ppjtIt seems awkward, compared to simply following down the package hierarchy
16:46ppjti.e. Serializer.Property
16:47rhickey_ppjt: the problem is Java uses '.' for so many things, some only distinguishable by static typing and context
16:49ppjtrhickey_: I see.
16:53ppjtThanks again for the help, &, surely, for Clojure! For me, it's been a way to do Lisp programming w/o special environment setup, which means Lisp programming with a chance of production use. It's been a real pleasure. Thanks for sharing your experience & effort with the community.
16:54rhickey_You're welcome!
17:14Chouserrhickey_: any conclusions on defns/ns/in-ns?
17:15ChouserMaybe that would be a conclujion
17:16rhickey_Chouser: I had it almost done yesterday but am concerned about the context of the refers, i.e. if you don't refer clojure before anything else then there isn't enough of Clojure to interpret ns/defns in loaded files
17:17Chouseryep, so you either need to always say clojure/defns, or have some other special mechanism (reader macro?)
17:17rhickey_clojure/defns is ugly
17:17ChouserI agree
17:18rhickey_The bigger issue is, what is the context of load? Should it be the ns that calls load, or a special read-only loading ns that ensures Clojure will be present to start with
17:19rhickey_the latter will dictate that all files start with some sort of ns declaration
17:20Chouserthat was part of why I asked about defns executing its flags in order -- if :refer-clojure (or the default) were always executed first, the rest of the items could assume clojure would be as much "there" as it ever will be for that file.
17:20Chouserhm -- I kinda like the special read-only ns.
17:20rhickey_right but if you don't want most of Clojure but need to load a lib that does...
17:21Chouseryeah, the problem is bigger than I had realized when I asked.
17:22rhickey_a known starting point is strong, only negative is does it impinge on more casual use, or is there a valid load-in-a-context scenario
17:22rhickey_things designed to be loaded in a context are probably broken anyway
17:23Chouserthe repl could start in 'user, thus have no extra burden. For any given .clj to require at least an (ns foo) at the top doesn't seem at all unreasonable.
17:23Chouserthis would only have to apply to things loaded via defns (and perhaps require, etc) -- load-file or other functions could of course be more basic and leave *ns* alone.
17:24Chouser...if you wanted to something unusual.
17:24Chouserare read-only ns's already supported, or would this be a new class implementing Namespace?
17:25rhickey_I don't think defn and the other loads are (or should be) any different
17:25rhickey_read-ony ns would need special support
17:28rhickey_I hate to go crazy over namespaces that don't include some core of clojure
17:29rhickey_There have been scenarios (NetKernel) where some supervisory loader knows the (tear-off) namespace that the loaded file doesn't
17:30Chouserall you really need is some way to get to defns and ns without depending on the current namespace.
17:30rhickey_and everything they expand into
17:30ChouserThe patch I sent you for ns uses fully-qualified names for all the expressions it generates.
17:31rhickey_that's good
17:35Chouserdefns and ns could be rolled into one, like (ns :create foo) vs. (ns foo). Then you'd only need a single reader macro.
17:37Chouser#!(:create foo (:import ...))
17:37rhickey_I don't know about a reader macro, but could have some special resolution to clojure/ns
17:38rhickey_I've been leaning towards ns/in-ns, where in-ns still takes symbol, and enabling all other ns- fns to take symbol or actual namespace
17:39rhickey_unqualified ns and in-ns could always resolve to clojure/ns and clojure/in-ns
17:39rhickey_probably easiest
17:53ChouserI guess that would behave essentially a special form, like "new"
17:53rhickey_less special than that
18:34rhickey_ok - try it out - added :refer-clojure support to ns, special resolution of ns and in-ns, moved to ns for inspector/parallel/set/xml/zip, rev 1017
23:30pjb3Is there a nice method to convert a keyword to a string without the colon?
23:30Chousername
23:30Chouseruser=> (name :foo)
23:30Chouser"foo"
23:44pjb3thx