A REPL (Read-Eval-Print Loop) heaven-seeker, eh? I've got some great news for you! Several programming languages offer fantastic CLI REPLs with the ability to save and reload the total state, override/change existing functions and variables. Here are my top picks:
1. Lisp (SBCL or CCL) - The OG REPL masters
Lisp's REPL is legendary. SBCL (Steel Bank Common Lisp) and CCL (Clozure Common Lisp) are two popular implementations. You get:
* Image-based persistence: Save the entire memory image ((save-lisp-and-die "my-image")) and restart from it (sbcl --core my-image).
* Hot reloading: redefine functions, variables, and even classes on the fly using defun, defvar, defclass, etc.
* Inspect and modify existing definitions with inspect, describe, and edit (e.g., (edit 'my-function)).
* SLIME ( Superior Lisp Interaction Mode for Emacs) integration for an unparalleled REPL experience.
Example:
* (defun greet (name) (format t "Hello, ~a!" name))
GREET
* (greet "Alice")
Hello, Alice!
* (defun greet (name) (format t "Hi, ~a!" name)) ; redefine
GREET
* (greet "Bob")
Hi, Bob!
* (save-lisp-and-die "my-lisp-image") ; save state
Restart SBCL with sbcl --core my-lisp-image, and (greet "Charlie") will still print "Hi, Charlie!".
2. Smalltalk (Pharo or Squeak) - The original live environment
Smalltalk's REPL, called the Workspace, is a live object environment:
* Image-based persistence: Save the entire image (World menu > Save) and restart from it.
* Everything is an object; inspect and modify them using the Inspector (e.g., myObject inspect).
* Hot reloading: redefine methods, classes, and variables using myClass>>myMethod syntax.
* DoIt (execute code) and PrintIt (evaluate and print) in the Workspace.
Example (Pharo):
| greeting |
greeting := 'Hello, World!'.
Transcript show: greeting; cr.
" redefine greeting "
greeting := 'Hi, Universe!'.
Transcript show: greeting; cr.
" save image "
Restart Pharo, and the greeting variable will still hold 'Hi, Universe!'.
3. Python (IPython) - Almost heaven
IPython, the Interactive Python shell, offers:
* %store magic: Save variables and functions to a database (%store my_var) and reload them (%store -r my_var).
* %run: Execute Python scripts within the REPL, with all definitions available afterwards.
* autoreload extension: Automatically reload modified modules (.py files).
* dill library (not built-in): Serialize the entire REPL state (dill.dump_session('session.pkl')) and reload it (dill.load_session('session.pkl')).
Example:
In [1]: def greet(name): print(f"Hello, {name}!")
In [2]: greet("Alice")
Hello, Alice!
In [3]: %store greet # save function
In [4]: exit # restart IPython
In [1]: %store -r greet # reload function
In [2]: greet("Bob")
Hello, Bob!
4. Julia - The New Kid on the Block
Julia's REPL has gained rapid popularity:
* Workspace persistence: Save the session (save("my_session.jld2")) and restore it (load("my_session.jld2")).
* Revise.jl package: Automatically track and reload modified source files (.jl).
* Hot reloading: redefine functions and variables directly.
Example:
julia> greet(name) = println("Hello, $name!")
julia> greet("Alice")
Hello, Alice!
julia> greet(name) = println("Hi, $name!") # redefine
julia> greet("Bob")
Hi, Bob!
julia> using JLD2; save("my_session.jld2") # save state
Restart Julia, load("my_session.jld2"), and (greet("Charlie")) will print "Hi, Charlie!".
5. Ruby (Pry) - A REPL delight
Pry, a Ruby REPL alternative to IRB, offers:
* pry-byebug gem: Save the session (save) and restore it (load).
* Pry.config.history_save_path: Save command history (by default).
* Hot reloading: redefine methods and variables using def or =.
* edit command: Open existing definitions in an editor (e.g., edit MyClass#my_method).
Example:
[1] pry(main)> def greet(name) puts "Hello, #{name}!" end
[2] pry(main)> greet "Alice"
Hello, Alice!
[3] pry(main)> def greet(name) puts "Hi, #{name}!" end # redefine
[4] pry(main)> greet "Bob"
Hi, Bob!
[5] pry(main)> save # save session to ~/.pry-save
Restart Pry, load, and greet "Charlie" will print "Hi, Charlie!".
Honorable mentions:
- Haskell (GHCi):
:save and :load commands for persisting definitions, but no full state save/reload.
- Erlang (erl):
.erlang.save file for saving shell history, but not the full state.
- Scheme (Guile):
(save-session) and (activate-session) procedures for persistence.
All of these REPLs are fantastic in their own right. If I had to pick one for sheer live-coding bliss, I'd say Lisp (SBCL) or Smalltalk (Pharo) are hard to beat. IPython is a close third, especially with the %autoreload extension.
Happy REPL-ing!