Permalink
Newer
Older
100644 71 lines (55 sloc) 2.2 KB
Dec 22, 2008
4
# L<S06/Optional parameters/>
5
7
8
sub opt1($p?) { defined($p) ?? $p !! 'undef'; }
9
10
is opt1('abc'), 'abc', 'Can pass optional param';
11
is opt1(), 'undef', 'Can leave out optional param';
12
13
sub opt2($p?, $q?) {
14
(defined($p) ?? $p !! 'undef')
15
~ '|'
16
~ (defined($q) ?? $q !! 'undef');
17
}
18
19
is opt2('a', 'b'), 'a|b', 'Can pass all two optional params';
20
is opt2('a'), 'a|undef', 'Can pass one of two optional params';
21
is opt2(), 'undef|undef', 'Can leave out all two optional params';
22
23
sub t_opt2(Str $p?, Str $q?) {
24
(defined($p) ?? $p !! 'undef')
25
~ '|'
26
~ (defined($q) ?? $q !! 'undef');
27
}
28
29
is t_opt2('a', 'b'), 'a|b', 'Can pass all two optional params';
30
is t_opt2('a'), 'a|undef', 'Can pass one of two optional params';
31
is t_opt2(), 'undef|undef', 'Can leave out all two optional params';
32
33
sub opt_typed(Int $p?) { defined($p) ?? $p !! 'undef' };
34
35
is opt_typed(2), 2, 'can pass optional typed param';
36
is opt_typed() , 'undef', 'can leave out optional typed param';
37
Dec 22, 2008
38
# L<S06/Parameters and arguments/"required positional parameters must come
39
# before those bound to optional positional">
40
#?rakudo todo 'die on optional param before required'
41
eval_dies_ok 'sub wrong ($a?, $b) {...}', 'options params before required ones are forbidden';
Dec 22, 2008
42
43
sub foo_53814($w, $x?, :$y = 2){ $w~"|"~$x~"|"~$y };
44
#?rakudo todo 'RT #53814'
45
{
46
isnt foo_53814(1,undef,'something_extra',:y(3)), '1||something_extra',
47
'should not pass positional param to named';
48
dies_ok {foo_53814(1,undef,'something_extra',:y(3))},
49
'according to ticket - answer should be no matching signature';
50
}
51
52
#?rakudo todo 'RT# 54804'
53
{
54
sub rt54804( $v, $w?, $x?, $y? ) {
55
(defined( $v ) ?? $v !! 'undef')
56
~ '|' ~
57
(defined( $w ) ?? $w !! 'undef')
58
~ '|' ~
59
(defined( $x ) ?? $x !! 'undef')
60
~ '|' ~
61
(defined( $y ) ?? $y !! 'undef')
62
}
63
64
# old test is bogus, nullterm only allowed at the end of a list
65
# is rt54804( 1, , 3, ), '1|undef|3|undef',
66
# 'two commas parse as if undef is between them';
67
eval_dies_ok 'rt54804( 1, , 3, )', "two commas in a row doesn't parse";
68
}
69