Skip to content

Commit

Permalink
Use kebob-case in example
Browse files Browse the repository at this point in the history
  • Loading branch information
zoffixznet committed May 6, 2017
1 parent 51f3789 commit d29b8b3
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions doc/Type/WhateverCode.pod6
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,45 @@ C<WhateverCode> parameters to do so, as in the following example:
has @.vals;
}
multi sub get_val(Cycle $c, Int $idx) {
multi sub get-val(Cycle $c, Int $idx) {
$c.vals[$idx % $c.vals.elems]
}
# Define what to do with a stand-alone * as the second argument
multi sub get_val(Cycle $c, Whatever $idx) {
get_val($c, $c.pos);
multi sub get-val(Cycle $c, Whatever $idx) {
get-val($c, $c.pos);
}
# Define what to do with a * in an expression
multi sub get_val(Cycle $c, WhateverCode $idx) {
get_val($c, $idx($c.pos));
# Define what to do with a * WhateverCode in an expression
multi sub get-val(Cycle $c, WhateverCode $idx) {
get-val($c, $idx($c.pos));
}
my Cycle $c .= new(:pos(2), :vals(0..^10));
say get_val($c, 3); # OUTPUT: «3␤»
say get_val($c, *); # OUTPUT: «2␤»
say get_val($c, *-1); # OUTPUT: «1␤»
say get-val($c, 3); # OUTPUT: «3␤»
say get-val($c, *); # OUTPUT: «2␤»
say get-val($c, *-1); # OUTPUT: «1␤»
Since C<WhateverCode> objects are C<Callable> you may use introspection
to create as fancy a behavior as you wish. Continuing the following
example we may add handling for two I<Whatever stars>:
=begin code :skip-test
# Define what to do with two * in an expression
multi sub get_val(Cycle $c, WhateverCode $idx where { .arity == 2 }) {
get_val($c, $idx($c.pos, $c.vals.elems));
multi sub 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
say get-val($c, * + * div 2); # 2 + 10/2 = 7
=end code
Note, though, that subexpressions may impose their own I<Whatever star>
rules:
=for code :skip-test
my @a = (0, 1, 2);
say get_val($c, @a[*-1]) # 2, because the star belongs to the Array class
say get-val($c, @a[*-1]) # 2, because the star belongs to the Array class
This can make the ownership of I<Whatever stars> become confusing rather
quickly, so be careful not to overdo it.
Expand Down

0 comments on commit d29b8b3

Please sign in to comment.