Rules / Types, classes & roles / basic

WhateverCode Reference

Code object constructed by Whatever-priming

What it is #

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

1 all-differ · 9 no-output · 1 not-runnable

class WhateverCode is Code { }

Not executed: the documentation states no expected output for this example.

class Cycle {
      has $.pos;
      has @.vals;
}

Not executed: the documentation states no expected output for this example.

multi get-val(Cycle $c, Int $idx) {
      $c.vals[$idx % $c.vals.elems]
}

Not executed: the documentation states no expected output for this example.

# Define what to do with a stand-alone * as the second argument
multi get-val(Cycle $c, Whatever $idx) {
    get-val($c, $c.pos);
}

Not executed: the documentation states no expected output for this example.

# Define what to do with a * WhateverCode in an expression
multi get-val(Cycle $c, WhateverCode $idx) {
    get-val($c, $idx($c.pos));
}

Not executed: the documentation states no expected output for this example.

my Cycle $c .= new(:pos(2), :vals(0..^10));

Not executed: the documentation states no expected output for this example.

say get-val($c, 3);   # OUTPUT: «3␤»
say get-val($c, *);   # OUTPUT: «2␤»
say get-val($c, *-1); # OUTPUT: «1␤»

Neither engine can run this in isolation — the example depends on context from the surrounding text.

# Define what to do with two * in an expression
multi get-val(Cycle $c, WhateverCode $idx where { .arity == 2 }) {
get-val($c, $idx($c.pos, $c.vals.elems));
}

say get-val($c, * + * div 2); # 2 + 10/2 = 7

Not executed: the documentation states no expected output for this example.

my @a = (0, 1, 2);
say get-val($c, @a[*-1]) # 2, because the star belongs to the Array class

Not executed: the documentation states no expected output for this example.

sub run-with-rand (Callable $code) { $code(rand) };
run-with-rand *.say;           # OUTPUT: «0.773672071688484␤»
run-with-rand {.say};          # OUTPUT: «0.38673179353983␤»
run-with-rand sub { $^v.say }; # OUTPUT: «0.0589543603685792␤»
Documentation
0.773672071688484
0.38673179353983
0.0589543603685792
Rakudo
0.29140375509459615
0.022983215076731733
0.5118635828926413
Raku++
0.21417195156950086
0.5413415939072337
0.6465300258841147

All three differ. Needs a human.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

sub run-with-rand (&code) { code time };

Not executed: the documentation states no expected output for this example.