Skip to content

Instantly share code, notes, and snippets.

@wjlroe
Created January 21, 2012 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wjlroe/1650711 to your computer and use it in GitHub Desktop.
Save wjlroe/1650711 to your computer and use it in GitHub Desktop.
metadata how does it work?
(defn
^{:user/help-msg "Exit back to your shell"}
exit
"exit the program"
[]
(System/exit 0))
@wjlroe
Copy link
Author

wjlroe commented Jan 21, 2012

When I compile that with slime/swank in emacs, then call (meta exit), that metadata isn't there. If I compile again - it is there. This means it doesn't work when running the code normally.

@et4te
Copy link

et4te commented Jan 21, 2012

user> (defn
  ^{:user/help-msg "Exit back to your shell"}
  exit
  "exit the program"
  [](System/exit 0))
# 'user/exit

user> (meta #'user/exit)
{:ns #<Namespace user>, :name exit, :file "NO_SOURCE_FILE", :line 1, :arglists ([]), :doc "exit the program", :user/help-msg "Exit back to your shell"}```

@et4te
Copy link

et4te commented Jan 21, 2012

Doing (meta exit) or even (meta user/exit) won't work here because we're looking for metadata on a var (functions are the equivalent of (def f (fn [] ...))).

user> exit
user/exit
user> #'exit (var user/exit)

@wjlroe
Copy link
Author

wjlroe commented Jan 21, 2012

Yeah, this was pointed out to me on #clojure. Seems obvious to me now :)

Problem is, now I can't work out how to have 2 vars that reference each other - seems the clojure compiler can only let you reference vars that were defined above you in the code.

I'e tried messing with (resolve (symbol "help")) to get around that but had no luck :(

@wjlroe
Copy link
Author

wjlroe commented Jan 21, 2012

Oh I've solved that now.

To have two vars that refer to each other, you need to define one first:

(def something)

(defn another []
   (let [blah (something)]
       blah))

(defn something []
   (format "hi: %s" (meta #'another)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment