Why does Rakudo dd return Nil for a typed and assigned scalar?

The REPL in Raku checks whether the code executed did any output to STDOUT. This is done with the assumption that if your code outputs something, that you would be interested in that, not the return value of the expression you just executed. So that is why:

> say 42
42

will only show 42 and not also show the return value of say (which happens to be True btw). It does not check STDERR. Check this with note:

> note 42
42
True

note is the same as say, but puts its output on STDERR instead of STDOUT. And the same applies to dd. So that's why you also get this with dd:

> dd 42
42
Nil

Except that the implementation of dd is returning Nil because it is intended as a debugging aid that should interfere as little as possible with its environment.


> sub mydd( $foo ) { dd $foo; "hello" }
&mydd
> mydd $x
1
hello

The Nil is the return value of dd, or lack thereof to be precise.


It's not "the output of dd" but rather output of the REPL based on the value returned by dd.

In more detail...

  • The R in REPL reads the line you enter (dd ...).

  • The E evaluates the line. It's dd, so it prints a line ending in a newline.

  • The P prints another line, also ending in a newline. If the evaluation in the prior step did not produce output on STDOUT, then the line this step produces includes the value returned by that evaluation.

  • (The L then loops around waiting for the next line of input.)

The first line you see after pressing Enter is output generated by dd.

The second line you see is output generated by the REPL.

By default, the content of the second line is the REPL's .gist of the value returned by the dd. The value dd returns is (always) Nil. So that's what you see.

(Liz has added a new environment variable since this SO was posted that lets users tell Rakudo what method to call; .gist is now merely the default.)

Tags:

Raku

Rakudo