<p>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 (<code>null</code>) or not to show it at all. For example, Python doesn't show any output when a <code>None</code>-returning expression is run in its REPL, but Node will always print <code>undefined</code> for expressions that return it. What are possible display options for nullish values in my REPL?</p>

<language-comparison><nullability><repl>
Should a REPL show null values as null?
CC BY-SA 4.0
2023-05-19T19:58:02.427Z
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](/help/dont-ask). Ex. What design considerations should answerers here attempt to evaluate the two approaches based on?
starball
<p>Many implementations of Scheme, among other Lisps, make a distinction between <code>null</code> (the constant value <code>()</code> which serves as both the empty list and the list terminator) and <code>void</code> (the constant value <code>#&lt;void&gt;</code> which is returned by side-effecting functions with no useful return value).</p>
<pre class="lang-scheme prettyprint-override"><code>&gt; '() ; yields null, which gets printed
()
&gt; (display &quot;Hello, LanguageDesign!\n&quot;) ; yields void, which does not get printed
Hello, LanguageDesign!
&gt;
</code></pre>
<p>By this convention, a function that can return <code>void</code> should <em>only</em> 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 <code>null</code> value.</p>

CC BY-SA 4.0
2023-05-20T08:26:18.777Z
2023-05-20T08:26:18.777Z
Olive
<p>Output the value if and only if you have multiple nullish types. Python has only <code>None</code> so it doesn't print to avoid clutter:</p>
<pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; x=2
&gt;&gt;&gt;
</code></pre>
<p>Python does have <code>NotImplemented</code> and <code>Ellipsis</code> but they are quite different from <code>None</code> and functions return <code>None</code> by default</p>
<p>However, javascript has both <code>null</code> and <code>undefined</code> so it needs to print undefined to disambiguate:</p>
<pre class="lang-js prettyprint-override"><code>&gt;&gt;&gt; let a = 2;
&lt;- undefined
</code></pre>
<p>As for the argument that you may want to re-use the value in a later expression: You can always get it back with <code>_</code> (in python) or <code>$_</code> (in node). If your language has a similar shortcut there is no need to print the value all the time.</p>

CC BY-SA 4.0
2023-05-19T18:23:10.957Z
2023-05-20T06:32:30.327Z
2023-05-20T06:32:30.327Z
mousetail 'he-him'
CC BY-SA 4.0
2023-05-19T18:36:42.237Z
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](https://stackoverflow.com/q/50374265/478288) 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.
chrisaycock
CC BY-SA 4.0
2023-05-19T18:39:25.633Z
@chrisaycock Maybe, but any function or expression that comes out to `None` has the same behavior
mousetail 'he-him'
<p>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.</p>
<pre><code>&gt;&gt;&gt; 5
5

&gt;&gt;&gt; &quot;Hello World&quot;
&quot;Hello World&quot;

&gt;&gt;&gt; null
null
</code></pre>
<p>So if your users can enter <code>null</code> onto the REPL, then <code>null</code> should be the output.</p>

CC BY-SA 4.0
2023-05-19T18:14:22.240Z
2023-05-19T18:53:03.807Z
2023-05-19T18:53:03.807Z
chrisaycock
CC BY-SA 4.0
2023-10-12T19:19:18.277Z
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
rydwolf
# Should a REPL show null values as null?
Posted by: username_0 on 2023-05-19
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?
**Tags:** language-comparison nullability repl
> **Comments:**
> **username_1**: 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](/help/dont-ask). Ex. What design considerations should answerers here attempt to evaluate the two approaches based on?  
---
## 3 answers
### Answer by username_4 on 2023-05-19. Score: 8
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.
> **Comments:**
> **username_5** (2023-10-12): 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  
---
### Answer by username_2 on 2023-05-20. Score: 6
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 # 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.
---
### Answer by username_3 on 2023-05-19. Score: 5
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.
> **Comments:**
> **username_4** (2023-05-19): 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](https://stackoverflow.com/q/50374265/478288) 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.  
> **username_3** (2023-05-19): @chrisaycock Maybe, but any function or expression that comes out to `None` has the same behavior  
---
