Shadowing global names

unsorted — cgrand, 31 January 2009 @ 10 h 45 min

A nice article on Clojure that gives one wrong tip: to use #'var-name to access a shadowed global.
Don’t do that! You’d better use namespace-where-defined-or-alias/var-name.
There are several reasons not to use #'var-name:

  • it doesn’t work if the value of your var is not invokable,
  • it evaluates to something different: it returns the var an not its value. It happens that vars proxy function calls to their values but a var can be rebound:
    (def a (map str (range 10)))
    (def b (map #'str (range 10)))
    (take 5 a)
    <i>("0" "1" "2" "3" "4")</i>
    (take 5 b)
    <i>("0" "1" "2" "3" "4")</i>
    (binding [str identity] (doall a))
    <i>("0" "1" "2" "3" "4" "5" "6" "7" "8" "9")</i>
    (binding [str identity] (doall b))
    <i>("0" "1" "2" "3" "4" <strong>5 6 7 8 9</strong>)</i>

NB: @#'var-name is better than #'var-name but, really, just use the namespaced symbol.

1 Comment »

  1. Thanks Christophe!

    I’ll correct it on the blog.

    Eric

    Comment by Eric — 2 February 2009 @ 23 h 26 min

RSS feed for comments on this post. TrackBack URI

Leave a comment

(c) 2017 Clojure and me | powered by WordPress with Barecity