On Sat, Mar 28, 2015 at 2:53 PM, Moritz Lenz <mor...@faui2k3.org> wrote:
> On 28.03.2015 12:27, Tom Browder wrote: > > I like the subroutine arg handling in Perl 6. Is there any simple > > way to attach a short error msg in place of or additive to the > > default for, say, a missing arg? > > You can always use multi subs, and use a catch-all candidate which > produces the error message. > > multi thingy($x) { $x + 42 } > multi thingy(|c) { die "Must call &thingy with exactly one argument" } Multis? I guess my mind went elsewhere: sidhekin@purplehat[00:25:02]~$ cat > tom.p6 sub foo($x = (warn "&foo called without argument; using default (42)"; 42) ) { $x + 42; } sub bar($x = (die "&bar called without argument; failing") ) { $x + 42; } say foo(-2); say foo(); say bar(-2); say bar(); sidhekin@purplehat[00:25:21]~$ perl6 tom.p6 40 &foo called without argument; using default (42) in sub foo at tom.p6:1 84 40 &bar called without argument; failing in sub bar at tom.p6:5 in block at tom.p6:12 sidhekin@purplehat[00:25:27]~$ Eirik