Is defn thread-safe?

后端 未结 2 1265
梦毁少年i
梦毁少年i 2021-01-04 14:02

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

2条回答
  •  天命终不由人
    2021-01-04 14:33

    Yes, it's thread safe.... but it does have side effects. Hence you may get unexpected results depending on what you are trying to do.

    In essence, defn on an existing function will rebind the corresponding var in the namespace.

    This means that:

    • Future accesses to the var will get the new version of the function
    • Existing copies of the old function that were previously read from the var will not change

    As long as you understand and are comfortable with that - you should be OK.

    EDIT: In response to Arthur's comment, here's an example:

    ; original function
    (defn my-func [x] (+ x 3))
    
    ; a vector that holds a copy of the original function
    (def my-func-vector [my-func])
    
    ; testing it works
    (my-func 2)
    => 5
    ((my-func-vector 0) 2)
    => 5
    
    ; now redefine the function
    (defn my-func [x] (+ x 10))
    
    ; direct call to my-func uses the new version, but the vector still contains the old version....
    (my-func 2)
    => 12
    ((my-func-vector 0) 2)
    => 5
    

提交回复
热议问题