I'm working on a REPL for a language that has a null value, and I'm not sure how to represent it in the output. The behavior of existing REPLs seems to be either to represent it by its name (null
) or not to show it at all. For example, Python doesn't show any output when a None
-returning expression is run in its REPL, but Node will always print undefined
for expressions that return it. What are possible display options for nullish values in my REPL?
-
1$\begingroup$ Please constrain your question. Ask us to evaluate responses in a way that fact-based. It is your job as a question asker to ask questions in a way that encourages objective answers or "good-subjective" answers. Ex. What design considerations should answerers here attempt to evaluate the two approaches based on? $\endgroup$– starballCommented May 19, 2023 at 19:58
3 Answers
The REPL's output and input should be the same. I should be able to copy an output and paste it back to the prompt.
>>> 5
5
>>> "Hello World"
"Hello World"
>>> null
null
So if your users can enter null
onto the REPL, then null
should be the output.
-
$\begingroup$ This sounds nice until you get to more complex data structures. Circular references, for example. I don't think this really works as a general rule $\endgroup$– rydwolfCommented Oct 12, 2023 at 19:19
Many implementations of Scheme, among other Lisps, make a distinction between null
(the constant value ()
which serves as both the empty list and the list terminator) and void
(the constant value #<void>
which is returned by side-effecting functions with no useful return value).
> '() ; yields null, which gets printed
()
> (display "Hello, LanguageDesign!\n") ; yields void, which does not get printed
Hello, LanguageDesign!
>
By this convention, a function that can return void
should only return void, so it's reasonable to not print a void return value at all in the REPL. This saves clutter at no expense to the potentially useful null
value.
Output the value if and only if you have multiple nullish types. Python has only None
so it doesn't print to avoid clutter:
>>> x=2
>>>
Python does have NotImplemented
and Ellipsis
but they are quite different from None
and functions return None
by default
However, javascript has both null
and undefined
so it needs to print undefined to disambiguate:
>>> let a = 2;
<- undefined
As for the argument that you may want to re-use the value in a later expression: You can always get it back with _
(in python) or $_
(in node). If your language has a similar shortcut there is no need to print the value all the time.
-
$\begingroup$ The reason
x=2
doesn't print anything in Python is that there isn't anything on the stack to be popped. This was a question I had about how Python knows when to print during REPL mode. It turns out that the codegen phase of the Python compiler appends an opcode to print whatever is on the top of the VM's stack. $\endgroup$ Commented May 19, 2023 at 18:36 -
$\begingroup$ @chrisaycock Maybe, but any function or expression that comes out to
None
has the same behavior $\endgroup$ Commented May 19, 2023 at 18:39