Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

raku

rakudo

The below is a REPL session using Rakudo.

> my Int $x = 1
1
> dd $x
Int $x = 1
Nil

Why is there a Nil on the second line of the output of dd?

like image 309
Ross Attrill Avatar asked May 20 '20 22:05

Ross Attrill


2 Answers

> 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.

like image 69
Holli Avatar answered Nov 11 '22 07:11

Holli


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.

like image 22
Elizabeth Mattijsen Avatar answered Nov 11 '22 07:11

Elizabeth Mattijsen