Based on your query, you're looking for programming languages with REPLs (Read-Evaluate-Print Loops) that make it relatively straightforward to save the current state of defined variables and functions (e.g., those created directly in the REPL session) and then reload them later. I'll focus on languages where this is possible without needing to handle external dependencies like FFI (Foreign Function Interface) or imported modules—just the in-REPL definitions.
Not all languages have built-in REPL support for this, as REPLs are often designed for interactive exploration rather than persistent state management. However, several languages do provide easy mechanisms, either through built-in commands or simple functions. Below, I'll list the languages that fit your criteria, along with a brief explanation of how to achieve this. I'll prioritize languages where the process is seamless and doesn't require much extra setup.
Languages with REPLs Supporting Easy Save and Reload
-
R
- REPL: The R console (accessed via
R in the terminal).
- How it works: R's REPL allows you to save the entire workspace (including all variables, functions, and objects defined in the session) using the
save.image() function. You can then load it back with load().
- Example:
- In the REPL, define some variables and functions, then run:
save.image(file = "my_workspace.RData").
- To reload: Start a new R session and run:
load("my_workspace.RData").
- This is one of the most straightforward implementations, as it captures the global environment by default.
- Limitations: It saves everything in the current environment, so be cautious if you have large objects.
- Why it fits: No need for external libraries; it's built-in and works directly in the REPL.
-
Julia
- REPL: Julia's interactive shell (accessed via
julia in the terminal).
- How it works: Julia provides serialization tools via its standard library or packages like JLD2 for saving and loading variables and functions. You can use
@save and @load from the JLD2 package to persist the state.
- Example:
- First, install the package if needed:
using Pkg; Pkg.add("JLD2").
- In the REPL, define variables and functions, then run:
using JLD2; @save "my_session.jld2" var1 var2 my_function.
- To reload: In a new session, run:
@load "my_session.jld2".
- If you want to save the entire workspace, you can serialize specific items manually.
- Limitations: JLD2 is a package, but it's commonly used and easy to add. This works well for in-REPL definitions.
- Why it fits: It's designed for interactive work and makes reloading simple, especially for scientific computing tasks.
-
MATLAB
- REPL: MATLAB's command window (the interactive environment).
- How it works: MATLAB has built-in functions to save and load variables and functions from the workspace.
- Example:
- In the REPL, define variables and functions, then run:
save('my_workspace.mat', 'var1', 'var2', 'my_function').
- To reload: In a new session, run:
load('my_workspace.mat').
- This saves the specified items (or all with
save('my_workspace.mat')) and restores them exactly.
- Limitations: It's proprietary software, so access might be limited, but if you have it, this is very user-friendly.
- Why it fits: It's a core feature of MATLAB's interactive environment, making it ideal for prototyping.
-
Octave (GNU alternative to MATLAB)
- REPL: Octave's interactive shell (accessed via
octave in the terminal).
- How it works: Similar to MATLAB, Octave allows you to save and load workspace variables using the
save and load commands.
- Example:
- In the REPL, define variables and functions, then run:
save -mat7-binary 'my_workspace.mat' var1 var2 my_function.
- To reload: In a new session, run:
load 'my_workspace.mat'.
- This works for most data types and functions defined in the session.
- Limitations: Like MATLAB, it might not handle very complex custom objects perfectly, but it's reliable for basic use.
- Why it fits: It's free, open-source, and provides the same save/load functionality as MATLAB.
-
Common Lisp (e.g., via SBCL or other implementations)
- REPL: The Lisp REPL (e.g., from SBCL, accessed via
sbcl in the terminal).
- How it works: Common Lisp implementations often support saving the entire image of the running environment, which includes all defined variables and functions.
- Example (in SBCL):
- In the REPL, define variables and functions, then run:
(sb-ext:save-lisp-and-die "my_image.core").
- To reload: Run
sbcl --core my_image.core to start a new session with the saved state.
- This effectively "freezes" the REPL state and restores it later.
- Limitations: The exact commands depend on the implementation (e.g., SBCL vs. CCL), and it saves the whole process, which might include more than just your definitions.
- Why it fits: Lisp's dynamic nature makes this a natural feature, and it's easy for in-REPL work.
-
Scheme (e.g., via Guile)
- REPL: Guile's interactive shell (accessed via
guile in the terminal).
- How it works: Some Scheme implementations, like Guile, allow you to dump and restore the environment using functions like
dump-port or serialization.
- Example:
- In the REPL, define variables and functions, then use:
(use-modules (ice-9 session)) and custom saving mechanisms, or manually serialize with (write object port).
- A simpler approach: Guile has experimental support for saving sessions via
(dump-core "file.go") in some versions.
- To reload: Use
(load "file.go") or similar.
- This isn't as polished as R or Julia but can be done with built-in tools.
- Limitations: It might require a bit more manual effort compared to other languages.
- Why it fits: For pure Scheme definitions, it's feasible and aligns with the language's interactive style.
Honorable Mentions
- IPython (for Python): While Python's standard REPL doesn't have built-in save/load, IPython (an enhanced REPL for Python) does. You can use the
%store magic command to save and load variables: e.g., %store var1 to save, and %store -r var1 to reload. However, this is more of an extension than the base Python REPL.
- Jupyter Notebooks (for Python, Julia, R, etc.): If you're open to a notebook-style interface (which includes a REPL-like experience), you can save and reload entire sessions as files. But since you specified REPLs, I assume you're referring to command-line ones.
General Advice
- Ease of Use: R and Julia are probably the easiest for your needs, as they require minimal code and work directly in the REPL.
- Cross-Platform Support: Most of these (e.g., R, Julia, Octave) are available on Windows, macOS, and Linux.
- Potential Drawbacks: Saving and reloading might not always preserve everything perfectly (e.g., due to serialization limitations), but for simple variables and functions, it should work well.
- Testing: I recommend trying these in a simple session to verify, as behavior can vary by version or implementation.
If you have a specific language in mind or additional constraints (e.g., focusing on open-source tools), let me know for more tailored advice!