3 ms·
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 decl
by _rf 3y ago
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.
- Galanwe 3y agoI guess the use case is to quickly try out some C/C++ code snippet. Typically I can think of checking binary arithmetic operations behavior. Say you have some buffer that you need to align on 4K blocks, you could quickly try out your "& ~0x1000" in the REPL. It may not be a convincing example, but you get the gist, sometimes you just want to quickly see the result of some one liners.
- PennRobotics 3y agoI did what you described just yesterday. I couldn't remember offhand if the compiler I was using would automatically convert a uint8_t to an int (because I didn't want to have to add two levels of parentheses by explicitly casting the high byte and then shifting) and very quickly threw together a // test if we need a<<8|b // or (a<<8)|b // or ((int)a<<8)|b // or (((int)a)<<8)|b #include <cstdio> #include <cstdint> int main() { uint8_t a = 2; uint8_t b = 38; printf("%d\n", a<<8|b); } More than half of the time is the uninteresting parts: writing the includes, creating a temp file, exiting to compile, forgetting a parenthesis, reediting and resaving, compiling again, running, deleting... In a REPL---particularly one with stdio/stdlib included by default---this would take 30 seconds but instead takes 3 or 4 minutes. It's not much, but tight feedback loops really, really keep you in a great mental state.
- galangalalgol 3y agoJulia compiles JIT to machine code, and I guess JS/TS and ruby too? All of those benefit from a repl for prototyping. I think the biggest fail with most repls is the transition from the repl when you have that moment of "it worked" to having the working bits minus the stuff around it crammed into an idiomatic reusable thing. It is fairly easy in matlab, but I haven't found a workflow in Julia that does that well, tried dr. Doc strings, pluto, and vscode plugin. I'd welcome a rust repl too.
- aaplok 3y agoWhat matlab workflow do you use that makes transitioning from REPL to file easy?
- galangalalgol 3y agoIts nothing earth shaking. I just type edit foo.m, select the lines from the history that worked and copy them to the new file, perhaps doing a quick namechange or two. It isn't anything a plugin could provide, and doctor doc strings tries to, but it is pretty clumsy, the name change refactoring thing isn't there, and more importantly, the idiomatic way to create a reusable module in julia is a lot heavier. I also run into issues of collisions between the new module and the lines I copied. I'm sure it is all resolvable, it just feels clunky out of the box after using it lightly for a year or so. I can use numpy the same way as matlab mostly, its just really slow. I haven't found a prototyping language other than matlab that I don't find myself thinking "might as well write it in rust as long as this is taking", as I know I'll end up doing that anyway.
- sundarurfriend 3y ago> the name change refactoring thing isn't there what does Matlab provide wrt this? Does its editor have refactoring assistance now? > the idiomatic way to create a reusable module in julia is a lot heavier do you mean package creation? > I also run into issues of collisions between the new module and the lines I copied. what kind of collisions do you mean? what would your ideal tool for this look like? you select parts of your REPL history, ask for some name changes in it, give it a package name, and have it generate a package? any other features or steps that come to mind as potentially helpful?
- inetknght 3y ago> I'm kind of confused what the use case of this would be. I imagine just about the same use case as running python3 interactively. Except, this is for C and C++. Sort've like Compiler Explorer but on the command line. Super cool.
- deleted 3y ago[deleted]
- dahart 3y ago> like... having a debugger attached to your program 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.