forked from Raku/roast
/
implicit-parameter.t
56 lines (44 loc) · 1.81 KB
/
implicit-parameter.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use v6;
use Test;
plan 18;
# L<S04/The Relationship of Blocks and Declarations/A bare closure
# (except the block associated with a conditional statement)>
{
# test with explicit $_
my $f1 = { 2*$_ };
is $f1(2), 4, 'Block with explicit $_ has one formal parameter';
}
{
# test with implicit $_
my $f2 = { .sqrt };
is_approx $f2(4), 2, 'Block with implicit $_ has one formal parameter';
}
{
# { } has implicit signature ($_ is rw = $OUTER::_)
$_ = 'Hello';
#?pugs todo 'feature'
is(try { { $_ }.() }, 'Hello', '$_ in bare block defaults to outer');
is({ $_ }.('Goodbye'), 'Goodbye', 'but it is only a default');
is({ 42 }.(), 42, 'no implicit $_ usage checking');
is({ 42 }.('Goodbye'), 42, '$_ gets assigned but is not used');
is(({ $_ }.arity), 0, '{$_} is arity 0, of course');
is(({ .say }.arity), 0, 'Blocks that uses $_ implicitly have arity 0');
is(({ $_ }.count), 1, '{$_} is count 1');
is(({ .say }.count), 1, 'Blocks that uses $_ implicitly have count 1');
}
{
#?pugs 4 todo 'pointy blocks'
$_ = 'Ack';
dies_ok({ (-> { "Boo!" }).(42) }, '-> {} is arity 0');
dies_ok({ (-> { $_ }).(42) }, 'Even when we use $_>');
#?rakudo 2 todo 'pointy blocks and $_'
is((-> { $_ }).(), 'Ack!', '$_ is lexical here');
is(-> $a { $_ }.(42), 'Ack!', 'Even with parameters (?)');
is(-> $_ { $_ }.(42), 42, 'But not when the parameter is $_');
eval_dies_ok( 'sub () { -> { $^a }.() }', 'Placeholders not allowed in ->');
is(-> { }.arity, 0, '->{} is arity 0, again');
}
{
eval_dies_ok('sub { $^foo }.(42)', 'Placeholders not allowed in sub()');
}
# vim: ft=perl6