| I'm kind of confused what the use case of this would be. So it's essentially saving you the trouble of having to put code into an "int main()" and putting declarations before it? And then attempt to run the code after every line you type. Are there other examples of REPLs that work like this rather than all the state of execution being held in the REPL like I imagine Python does do it? Can it Tab-complete identifiers I introduce in it? Actually, I suppose it's a single 115 LoC bash script, and I really don't mean to dunk on it, it's a neat little experiment. It just kinda made me wonder what REPLs are made for, for the first time. :D Like I imagine a very basic "interactive" coding session (for any language) could also be achieved by having an editor open and saving after each line, or having that done automatically, and then in another terminal some script watches for those changes and compiles/interprets/runs it. A REPL like python's is more like... having a debugger attached to your program, but it just sits at a breakpoint and let's you tell it what code to run next, and you can inspect variable contents and such. Seems like languages that compile to machine code don't work all that well for REPLing, eh. |
Maybe this answers your own question, it’s a great reason to use a REPL, and this is a good analogy.
One thing REPLs are good for is the ability to interactively work through syntax errors. Type a line, it has a problem, so just do it again. It doesn’t have to completely stop and start over like a compiler. Syntax errors are not a big deal for a small test file, but can be very annoying if you’re working in a big project with a big build system.
I’d say not having to save a file, not having to write the command line to compile it to another file, not having to write the command line to run it (while usually leads me to write a small makefile if I have to do it more than a few times), all in addition to not having to wrap the code in “int main()” all adds up to a nice convenient way to run small snippets of C/C++. It might be extra useful to optionally auto-include the most common headers, and perhaps offer some shortcuts for common things.
I test little snippets all the time like printing the bits in a floating point number, or checking the syntax for struct packing, or dumping a quick table of numbers run through a function, stuff like that. It’s a pretty common occurrence for me to want to run a small amount of C++ code without any workflow overhead.
I also use Python and JavaScript REPLs all the time too for quick tests, even though they also take files, and compile them.(!) All language compile to machine code, so I’m not sure I agree that has to do with how well a language works in a REPL. Python’s really not that great in a REPL, IMO: you can’t easily paste copied code into it because the indentation is usually wrong.