Re: Custom errors on subsets?

2010-01-05 Thread John Harrison
Would it make sense to have the failure bound to the function parameter
rather than the subset? eg.:

sub foo (Str $name where { $_ ~~ :f } else { die "Houston, we don't have a
file" } ) { ... }

Just a thought...

On Tue, Jan 5, 2010 at 1:07 AM, Ovid
wrote:

> --- On Mon, 4/1/10, yary  wrote:
>
> > From: yary 
>
> > How about
> > multi sub foo(Any $name) { die "Houston, we have a major
> > malfunction."}
>
> Looks like tha would work, but it forces the developer to remember to write
> this extra code every time they may have a constraint failure, if they
> forget, we're back to the old, cryptic message.  It would be much nicer to
> be able to do this (psuedo-code, obviouly):
>
>  subset Filename of Str where { $_ ~~ :f }
> :OnFail { "No such file: '$_'" }
>  subset Celsius  of Num where { $_ >= -273.15 }
>:OnFail { "Celsius temperature should be a Num >= -273.15, not '$_' " }
>
> With something akin to that, developers won't have to write extra
> boilerplate every time a constraint fails.  Plus, the code is friendlier :)
>
> Cheers,
> Ovid
> --
> Buy the book - http://www.oreilly.com/catalog/perlhks/
> Tech blog- http://use.perl.org/~Ovid/journal/
> Twitter  - http://twitter.com/OvidPerl
> Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
>
>
>


-- 
John "ash" Harrison


Re: Custom errors on subsets?

2010-01-05 Thread Jon Lang
Perhaps you could create an error function that temporarily sets the
default error message (does perl 6 still have the $! variable?) and
returns false; so:

subset Filename of Str where { $_ ~~ :f or error ( "No such file: '$_'" ) }

Of course, that's a rather narrowly-defined function, as it's intended
only for use in one situation - that is, where a boolean test is
likely to be used by the parser to determine whether or not Something
Bad needs to happen.  I can't think of any case other than custom
error messages for subsets where this would happen...

Personally, I'd prefer something more along the lines of:

subset Filename of Str where { $_ ~~ :f; ERROR { "No such file: '$_'" } }

That is, have the parser look inside where blocks (or any other block
that might convert a falsehood to a failure) for an ERROR block that
is intended to be executed just as the error is about to be triggered.
 The primary purpose would be to supply a customized error message
(and for that reason, the return value should be a string that can be
used as $!); but other possibilities would exist.

-- 
Jonathan "Dataweaver" Lang


Re: Custom errors on subsets?

2010-01-05 Thread Jonathan Scott Duff
On Tue, Jan 5, 2010 at 1:07 AM, Ovid
wrote:

> --- On Mon, 4/1/10, yary  wrote:
>
> > From: yary 
>
> > How about
> > multi sub foo(Any $name) { die "Houston, we have a major
> > malfunction."}
>
> Looks like tha would work, but it forces the developer to remember to write
> this extra code every time they may have a constraint failure, if they
> forget, we're back to the old, cryptic message.  It would be much nicer to
> be able to do this (psuedo-code, obviouly):
>
>  subset Filename of Str where { $_ ~~ :f }
> :OnFail { "No such file: '$_'" }
>  subset Celsius  of Num where { $_ >= -273.15 }
>:OnFail { "Celsius temperature should be a Num >= -273.15, not '$_' " }
>
> With something akin to that, developers won't have to write extra
> boilerplate every time a constraint fails.  Plus, the code is friendlier :)
>

I'd imagine that the functionality will fall out of the ability  to have
nice failures because surely something like the following works now:

subset Filename of Str where { $_ ~~ :f  or fail "No such file: '$_'" }

Perhaps s/fail/die/, but that seems like a means to your desired end.

-Scott
-- 
Jonathan Scott Duff
perlpi...@gmail.com


Re: Custom errors on subsets?

2010-01-05 Thread Larry Wall
On Tue, Jan 05, 2010 at 11:52:42AM -0500, Solomon Foster wrote:
: On Tue, Jan 5, 2010 at 11:36 AM, Ovid
:  wrote:
: > --- On Tue, 5/1/10, Solomon Foster  wrote:
: >
: >> From: Solomon Foster 
: >
: >> > Is this a bug or just documented behavior that I don't
: >> know about?
: >>
: >> fail just returns an uncalled exception.  What does
: >> that do in a where block?
: >
: > I knew it returned an uncalled exception, but I'm still not expecting the 
sub call to be skipped silently due to a constraint failure.  Silent failures 
are bad :)
: 
: Good point.  :)

I think it's fine to return a failure, but we'll need to distinguish two
different kinds of constraint checks, mandatory vs discretionary.  In

my Constraint $x = 42;

the Constraint is mandatory, and should die if the constraint check
returns a failure.  In the typical multi-dispatch, however, you
just want a failure to mean "look elsewhere".  (You can still
force a die there if you really want it.)

To the first approximation, a mandatory check requires a defined
"where" return value, while a discretionary check only requires a
true value.

(Note that a constraint on the parameter of an "only" sub is already
mandatory, not discretionary, at least in the sense that the call
will fail if the binding is unsuccessful.  So that probably falls
out naturally, though perhaps loses track of the failure message
currently.)

Larry


Re: Custom errors on subsets?

2010-01-05 Thread Solomon Foster
On Tue, Jan 5, 2010 at 11:36 AM, Ovid
 wrote:
> --- On Tue, 5/1/10, Solomon Foster  wrote:
>
>> From: Solomon Foster 
>
>> > Is this a bug or just documented behavior that I don't
>> know about?
>>
>> fail just returns an uncalled exception.  What does
>> that do in a where block?
>
> I knew it returned an uncalled exception, but I'm still not expecting the sub 
> call to be skipped silently due to a constraint failure.  Silent failures are 
> bad :)

Good point.  :)

-- 
Solomon Foster: colo...@gmail.com
HarmonyWare, Inc: http://www.harmonyware.com


Re: Custom errors on subsets?

2010-01-05 Thread Ovid
--- On Tue, 5/1/10, Solomon Foster  wrote:

> From: Solomon Foster 

> > Is this a bug or just documented behavior that I don't
> know about?
> 
> fail just returns an uncalled exception.  What does
> that do in a where block?

I knew it returned an uncalled exception, but I'm still not expecting the sub 
call to be skipped silently due to a constraint failure.  Silent failures are 
bad :)

Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog- http://use.perl.org/~Ovid/journal/
Twitter  - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6




Re: Custom errors on subsets?

2010-01-05 Thread Solomon Foster
On Tue, Jan 5, 2010 at 11:18 AM, Ovid
 wrote:
> --- On Tue, 5/1/10, Jonathan Scott Duff  wrote:
>
>> From: Jonathan Scott Duff 
>
>> I'd imagine that the functionality will fall
>> out of the ability  to have nice failures because surely
>> something like the following works now:
>>
>> subset Filename of Str where { $_ ~~ :f  or
>> fail "No such file: '$_'" }
>> Perhaps s/fail/die/, but that seems like a means
>> to your desired end.
>
> Ah, the die works fine.  However, "fail" causes that sub call to be skipped 
> altogether:
>
>    subset Filename of Str where { $_ ~~ :f or fail "$_ is not a filename" };
>
>    sub foo (Filename $name) {
>        say "Houston, we have a filename: $name";
>    }
>
>    foo($*EXECUTABLE_NAME);
>    say "before";
>    foo('no_such_file');
>    say "after";
>
> Output:
>
>    Houston, we have a filename: /Users/ovid/bin/perl6
>    before
>    after
>
> Is this a bug or just documented behavior that I don't know about?

fail just returns an uncalled exception.  What does that do in a where block?

-- 
Solomon Foster: colo...@gmail.com
HarmonyWare, Inc: http://www.harmonyware.com


Re: Custom errors on subsets?

2010-01-05 Thread Ovid
--- On Tue, 5/1/10, Jonathan Scott Duff  wrote:

> From: Jonathan Scott Duff 

> I'd imagine that the functionality will fall
> out of the ability  to have nice failures because surely
> something like the following works now:
> 
> subset Filename of Str where { $_ ~~ :f  or
> fail "No such file: '$_'" }
> Perhaps s/fail/die/, but that seems like a means
> to your desired end. 

Ah, the die works fine.  However, "fail" causes that sub call to be skipped 
altogether:

subset Filename of Str where { $_ ~~ :f or fail "$_ is not a filename" };

sub foo (Filename $name) {
say "Houston, we have a filename: $name";
}

foo($*EXECUTABLE_NAME);
say "before";
foo('no_such_file');
say "after";

Output:

Houston, we have a filename: /Users/ovid/bin/perl6
before
after

Is this a bug or just documented behavior that I don't know about?

Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog- http://use.perl.org/~Ovid/journal/
Twitter  - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6




Re: Custom errors on subsets?

2010-01-04 Thread Ovid
--- On Mon, 4/1/10, yary  wrote:

> From: yary 

> How about
> multi sub foo(Any $name) { die "Houston, we have a major
> malfunction."}

Looks like tha would work, but it forces the developer to remember to write 
this extra code every time they may have a constraint failure, if they forget, 
we're back to the old, cryptic message.  It would be much nicer to be able to 
do this (psuedo-code, obviouly):

  subset Filename of Str where { $_ ~~ :f } 
:OnFail { "No such file: '$_'" }
  subset Celsius  of Num where { $_ >= -273.15 }
:OnFail { "Celsius temperature should be a Num >= -273.15, not '$_' " }

With something akin to that, developers won't have to write extra boilerplate 
every time a constraint fails.  Plus, the code is friendlier :)

Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog- http://use.perl.org/~Ovid/journal/
Twitter  - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6




Re: Custom errors on subsets?

2010-01-04 Thread yary
On Mon, Jan 4, 2010 at 5:15 AM, Ovid
 wrote:
> Given this code:
>
>subset Filename of Str where { $_ ~~ :f };
>
>sub foo (Filename $name) {
>say "Houston, we have a filename: $name";
>}
...
> Obviously the error message can use some work, but how would I customize that 
> error message (assuming such will be possible in the future)?  Clearly there 
> will be many cases where a custom error message for constraint failure could 
> make life much easier for developers.


How about
multi sub foo(Any $name) { die "Houston, we have a major malfunction."}

-y


Custom errors on subsets?

2010-01-04 Thread Ovid
Given this code:

subset Filename of Str where { $_ ~~ :f };

sub foo (Filename $name) {
say "Houston, we have a filename: $name";
}

my Filename $foo = $*EXECUTABLE_NAME;
foo($foo);
foo($*EXECUTABLE_NAME);
foo('no_such_file');

We get this output:

Houston, we have a filename: /Users/ovid/bin/perl6
Houston, we have a filename: /Users/ovid/bin/perl6
Constraint type check failed for parameter '$name'
in Main (file src/gen_setting.pm, line 324)

Obviously the error message can use some work, but how would I customize that 
error message (assuming such will be possible in the future)?  Clearly there 
will be many cases where a custom error message for constraint failure could 
make life much easier for developers.

Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog- http://use.perl.org/~Ovid/journal/
Twitter  - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6