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

  • SEARCH
  • Home
  • 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 4017658
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:53:17+00:00 2026-05-20T09:53:17+00:00

Can I redefine function in real-time without side effects? Is defn thread-safe?

  • 0

Can I redefine function in real-time without side effects? Is defn thread-safe?

  • 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-05-20T09:53:18+00:00Added an answer on May 20, 2026 at 9:53 am

    “thread safe enough for development, not for use in production.”

    using defn to redefine functions can break functions that call it if they are running while the call changes. it’s ok in development because you can just restart after it breaks. It’s safe enough if you can control when the function you are changing is called.

    defn is a macro that resolves to somthing like

    (def name (fn [args] (code-here)))
    

    so it creates an instance of a function and then puts it into the root binding of a var. vars are a mutable data structure to allow for per-thread values. so when you call defn that assigns the base value that all threads will see. if another thread then changed the var to point at some other function it would change it’s copy with out affecting any other threads. all the old threads would still see the old copy

    When you re-bind the root value of a var by calling def again (through the defn macro) you change the value that every thread which has not set it’s own value will see. threads that have decided to set their own values will continue to see the value they themselves set and not have to worry about the value being changed out from under them.


    single thread no race

    When a function call is made the current value of the var with the name of the function, as seen by the thread doing the calling (this is important), is used. so if the value of the var changes then all future calls will see the new value; but they will only see changes to the root binding or their own thread-local binding. so first the normal case with only a root binding:

    user=> (defn foo [] 4)
    #'user/foo
    user=> (defn bar [] (foo))
    #'user/bar
    user=> (bar)
    4
    user=> (defn foo [] 6)
    #'user/foo
    user=> (bar)
    6
    

    two threads, still no race

    then we run another thread and in that thread redefine foo to return 12 instead

    user=> (.start (Thread. (fn [] (binding  [foo (fn [] 12)] (println (bar))))))
    nil
    user=> 12
    

    the value of foo (as seen by bar) is still unchanged in the first thread (the one running the repl)

    user=> (bar)
    6
    user=> 
    

    two threads and a race condition

    next we will change the value of the root binding out from under a thread with no local binding and see that the value of the function foo changes half way through a function running in another thread:

    user=> (.start (Thread. (fn [] (println (bar)) 
                            (Thread/sleep 20000) 
                            (println (bar)))))                        
    nil
    user=> 6                ;foo at the start of the function
    
    user=> (defn foo [] 7)  ;in the middle of the 20 seond sleep we redefine foo
    #'user/foo
    user=> 7                ; the redefined foo is used at the end of the function
    

    If the change to foo (which is called indirectly) had changed the number of arguments this would have been a crash instead of a wrong answer (which is arguably better). At this point it is fairly clear that somthing needs to be done if we want to use vars and devn for changing our functions.


    how to use vars with no race condition

    You really may want functions to not change mid call so you can use a thread-local binding to protect your self from this by changing the function running in the new thread to save the current value of foo into its thread-local bindings:

    user=> (.start (Thread. (fn [] (binding [foo foo] (println (bar)) 
                                                      (Thread/sleep 20000)
                                                      (println (bar))))))
    nil
    user=> 7
    
    user=> (defn foo [] 9)
    #'user/foo
    user=> 7
    

    The magic is in the expression (binding [foo foo] (code-that-uses-foo)) this could be read as “assign a thread local value to foo of the current value of foo” that way it stays consistent until the end of the binding form and into anything that is called from that binding form.


    Clojure gives you choices, but you must choose

    vars are good enough to hold your functions and redefine them to your hearts content while developing code. using code to automatically redefine functions very quickly on a deployed system using vars would be less wise. Not because vars are not thread safe, but because in this context vars are the wrong mutable structure to hold your function. Clojure has mutable structure for every use case and in the case of rapid automated editing of functions that need to stay consistent through the running of a transaction you would do better to hold you’re functions in refs. What other language lets you choose the structure that holds your functions!*

    • not a real question, just about any functional language can do this
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read a lot that LISP can redefine syntax on the fly, presumably
In vim, in my .vimrc, how can I redefine a command (i.e. :e) as
In a derived class If I redefine/overload a function name from a Base class,
Is there any way to redefine a class or some of its methods without
I need to know if a redefined function in Eiffel can define its own
I can access a python function's attribute inside of function itself by below code:
When doing calculations in Mathematica I often need to redefine a function by appending
In C/C++ you can implement a direct threaded interpreter with an array of function
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of

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.