Re: Fancy sub arg handling: ability to expand error message?

2015-03-29 Thread Tom Browder
On Sat, Mar 28, 2015 at 5:43 PM, Tom Browder  wrote:
> Agree--looks like we need some of Damien Conways wonderful CPAN modules

That should be "Damian Conway's"

-Tom


Re: Fancy sub arg handling: ability to expand error message?

2015-03-28 Thread The Sidhekin
On Sat, Mar 28, 2015 at 2:53 PM, Moritz Lenz  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


Re: Fancy sub arg handling: ability to expand error message?

2015-03-28 Thread Tom Browder
On Mar 28, 2015 8:54 AM, "Moritz Lenz"  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" }
>
> Though IMHO that's usually not worth the trouble; you get the error
> message only for programming errors, not for user errors; and
> programmers should be able to understand the error message (or we need
> to improve the error messages, if that's not the case).
>
> Also it makes it harder for others to extend your API by providing
> additional multi candidates.

Agree--looks like we need some of Damien Conways wonderful CPAN modules
ported ASAP!

Cheers!

-Tom


Re: Fancy sub arg handling: ability to expand error message?

2015-03-28 Thread Moritz Lenz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi,

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" }

Though IMHO that's usually not worth the trouble; you get the error
message only for programming errors, not for user errors; and
programmers should be able to understand the error message (or we need
to improve the error messages, if that's not the case).

Also it makes it harder for others to extend your API by providing
additional multi candidates.

Cheers,
Moritz
-BEGIN PGP SIGNATURE-
Version: GnuPG v1

iEYEARECAAYFAlUWsnQACgkQT81LMIj/VkTbAwCfTsQumtjPKj1lxXZlnQ+U+0Xz
uTQAn287aU2xU7m6iMFGWD+j2R+Bouy6
=LWsI
-END PGP SIGNATURE-