Editing programs “while they are running”? How?

后端 未结 8 2152
夕颜
夕颜 2021-02-13 06:19

This question is a corollary to: Editing programs “while they are running”? Why?

I\'m only recently being exposed to the world of Clojure and am fascinated by a few exam

8条回答
  •  無奈伤痛
    2021-02-13 07:15

    It's possible in many languages, but only if you have the following features:

    • Some form of REPL or similar so you can interact with the running environment
    • Some form of namespace that can be modified at runtime
    • Dynamic binding against the namespace, so that if you change items in the namespace then referring code automatically picks up the change

    Lisp/Clojure has all of these built in by default, which is one of the reasons why it is particularly prominent in the Lisp world.

    Example demonstrating these features (all at the Clojure REPL):

    ; define something in the current namespace
    (def y 1)
    
    ; define a function which refers to y in the current namespace
    (def foo [x] (+ x y))
    
    (foo 10)
    => 11
    
    ; redefine y
    (def y 5)
    
    ; prove that the change was picked up dynamically
    (foo 10)
    => 15
    

提交回复
热议问题