Hi Jamie,

Thats interesting. How do I not let the compiler not optimize away the fact
that y refers to the constant. When I run your program I have two constant
fields defined rather than 'y. Am I missing something.

Thanks
Guru


On Tue, Nov 26, 2013 at 6:16 AM, Jamie Brandon <ja...@scattered-thoughts.net
> wrote:

> > (def f (let [y 1] (fn [x] (+ x y))))
>
> Let's have a look inside.
>
> > (require '[no.disassemble :refer [disassemble]])
> > (println (disassemble f))
> // Compiled from /home/jamie/strucjure/src/strucjure.clj (version 1.5
> : 49.0, super bit)
> public final class strucjure$fn__5589$fn__5590 extends
> clojure.lang.AFunction {
>
>   // Field descriptor #7 Lclojure/lang/Var;
>   public static final clojure.lang.Var const__0;
>
>   // Field descriptor #25 Lclojure/lang/IPersistentMap;
>   final clojure.lang.IPersistentMap __meta;
>
>   // Field descriptor #27 J
>   long y;
> ....
>
> So there is the field you want.
>
> > (.y f)
> java.lang.IllegalArgumentException: No matching field found: y for
> class strucjure$eval5552$fn__5553 Reflector.java:271
> clojure.lang.Reflector.getInstanceField Reflector.java:300
> clojure.lang.Reflector.invokeNoArgInstanceMember
> /home/jamie/strucjure/src/strucjure.clj:21 strucjure/eval5556
>
> Humbug.
>
> > (require '[clojure.reflect :refer [reflect]])
> > (reflect f)
> java.lang.ClassNotFoundException: strucjure$eval5552$fn__5553
>
> Interesting... I would love to know why that is the case.
>
> > (into [] (.. f getClass getDeclaredFields))
>
> [#<Field public static final clojure.lang.Var
> strucjure$fn__5589$fn__5590.const__0> #<Field final
> clojure.lang.IPersistentMap strucjure$fn__5589$fn__5590.__meta>
> #<Field long strucjure$fn__5589$fn__5590.y>]
>
> > (defn get-field [instance field-name]
>   (let [field (first (filter #(= field-name (.getName %)) (.. instance
> getClass getDeclaredFields)))]
>     (.setAccessible field true)
>     (.get field instance)))
> > (get-field f "y")
> 1
>
> Success! And just for fun:
>
> > (def f2 (strucjure$fn__5589$fn__5590 7))
> > (f2 1)
> 8
>
> I hope that helps. Now the more interesting question is why you would
> want to do that...
>
>
> On 21 November 2013 18:14, henry w <henryw...@gmail.com> wrote:
> > Say you have a function created like this:
> >
> > (fn [x] (+ x y))
> >
> > and then you have a reference to an instance of this function, is there
> some
> > way to use the function reference to access the list '(fn [x] (+ x y)),
> > including the symbol 'y' (such that you could dereference 'y' and get its
> > value)? The source function is not the right thing and so far googling
> > hasn't got me the answer.
> >
> > Thanks
> >
> > --
> > --
> > 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
> > Note that posts from new members are moderated - please be patient with
> your
> > first post.
> > 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
> > ---
> > You received this message because you are subscribed to the Google Groups
> > "Clojure" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to clojure+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/groups/opt_out.
>
> --
> --
> 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
> Note that posts from new members are moderated - please be patient with
> your first post.
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to