I'm making my little toy stack-language, and am revolving it around a central concept of Bytecode, literally just like the JVM.
One of the opcodes I want to add in, is a print_var
function, that will print the value of a local variable. My question is, which print function do I use? println()
, or print()
?
Well really, it's not a question of what to use, but what is the standard in actual applications of this process? Are systems designed to simply print()
, and then a println()
function is built around the print()
function, or are they both made hand-in-hand to do seperate things?
I'm taking a look at Java's PrintWriter
source, which is used in System.out.println()
, and its defined as follows:
public void println(boolean x) {
synchronized (lock) {
print(x);
println();
}
}
Obviously Java made a print()
function and based the println()
off of it. Are many other major languages like that?
Let's see:
Long story short: you can find both. So simply implement what you think works best for your requirements respectively your approach in general. In other words: simply have a look at the other opcodes you have/want to implement and assess if they are more about "convenience" (so you would have print and println), or if they focus on providing "elementary building blocks" (then you might only provide print).
Beyond that: keep in mind that providing println() has one major advantage: the user doesn't need to worry about different "new line" characters for different operating systems.