Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:26:48+00:00 2026-06-10T02:26:48+00:00

Given that this works as I’d expect: (do (println (resolve ‘a)) ; nil (def

  • 0

Given that this works as I’d expect:

(do
  (println (resolve 'a)) ; nil 
  (def a "a")
  (println (resolve 'a))) ; #'user/a

I’d like to understand why this doesn’t:

(future
  (println (resolve 'b)) ; #'user/b (shouldn't it be still undefined at this point?)
  (def b "b")
  (println (resolve 'b))) ; #'user/b

I’d also like to know if this is a proper solution (not exactly solving the same problem, but doing an equivalent job in my context):

(def c (atom nil))
(future
  (println @c) ; nil
  (reset! c "c")
  (println @c)) ; c
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:26:50+00:00Added an answer on June 10, 2026 at 2:26 am

    This behaviour comes about as a result of the way in which def forms are compiled.

    Note that using def forms not at top-level (or perhaps inside a top-level let — see below for more comments on this case) is frowned upon as a matter of style in any case. The snippet using an Atom, on the other hand, is fine — no reason not to use it if it does what you want.

    On to the def story:

    1. Compilation of def forms:

      When a def form is encountered, a Var of the appropriate name is created at that moment by the compiler in the current namespace. (Attempting to def a Var outside the current namespace by using a namespace-qualified symbol as the name argument to def results in an exception). That Var is at first unbound and stays unbound until the def is actually executed; for a top-level def, that’ll be right away, but for a def hidden inside a function’s body (or inside a let form — see below), that’ll be when the function is called:

      ;;; in the user namespace:
      
      (defn foo []
        (def bar "asdf")
       :done)
      ; => #'user/foo
      
      bar
      ; => #<Unbound Unbound: #'user/bar>
      
      ;;; let's change the namespace and call foo:
      
      (ns some.ns)
      (user/foo)
      ; => :done
      
      bar
      ; exception, the bar Var was created in the user namespace!
      
      user/bar
      ; => "asdf"
      ; the Var's namespace is fixed at compile time
      
    2. The first example — with the do form:

      Top level dos are treated as if their contents were spliced into the flow of code at the place where the do occurs. So if you type (do (println ...) (def ...) (println ...)) at the REPL, that’s equivalent to typing in the first println expression, then the def, then the second println expression (except the REPL only produces one new prompt).

    3. The second example — with future:

      (future ...) expands to something close to (future-call (fn [] ...)). If ... includes a def form, it’ll be compiled in the manner we have seen above. By the time the anonymous function executes on its own thread the Var will have been created, thus resolve will be able to find it.

    4. As a side note, let’s have a look at a similar snippet and its output:

      (let []
        (println (resolve 'c)) 
        (def c "c") 
        (println (resolve 'c)))
      ; #'user/c
      ; #'user/c
      ; => nil
      

      The reason is as before with the extra point that let is first compiled, then executed as a whole. This is something one should keep in mind when using top-level let forms with definitions inside — it’s generally ok as long as no side-effecty code is intermingled with the definitions; otherwise one has to be extra careful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that I have this resultset structure (superfluous fields have been stripped) Id |
Given that django-nonrel has got JOINs working, does this mean we have M2M fields
datetime.datetime.utcnow() Why does this datetime not have any timezone info given that it is
I need to reduce this boolean expression to its simplest form. Its given that
Given this hash that comes though :params positions=>{ 1=>{x=>50, y=>30}, 2=>{x=>22, y=>53}, 3=>{x=>68, y=>35}
So I wrote this function that is given possible numbers, and it has to
I have this application that will recurse all folders in a given directory and
I was given this structure of a webservice that i need to call to.
I was given this Python code that would calculate an MD5 value for any
Given this SQL: SELECT * FROM mytable ORDER BY mycolumn, RAND() Assuming that mycolumn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.