Re: Contextual::Return (was Re: could 'given' blocks have a return value?)

2006-08-31 Thread Juerd
Damian Conway skribis 2006-08-31  9:08 (+1000):
 return want.rw ?? $lvalue
 :: want.count == 2 ?? (7,11)
 :: want.item   ?? 42
 :: want.list   ?? 1..10
 ::die Bad context;

s:g/::/!!/  # :)


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html


Contextual::Return (was Re: could 'given' blocks have a return value?)

2006-08-30 Thread Trey Harris

In a message dated Tue, 29 Aug 2006, Mark Stosberg writes:

my $rm = sub { given $rm_param {
   when Code  { $rm_param(self)   }
   when Hash  { %rm_paramrun_mode }
   default{ self.query.param($rm_param) }
   }}();


This is eerily like Contextual::Return, which made me wonder if it's even 
required in Perl 6.


Obviously we can do

return do given want {
  when :($) { ... }
  ...
};

But return do given want flows so badly, I desperately want some sugar 
for this.  Is there some and I missed it?


Trey



Re: could 'given' blocks have a return value?

2006-08-30 Thread Mark Stosberg
Agent Zhang wrote:

 
 According to S04, given {} is at statement level, so you can't use it
 directly as an expression. But Perl 6 always allow you to say
 
  my $foo = do given {...}
 
 As well as
 
  my $foo = do if foo {...} else {...}

I confirmed this both work now with pugs!

I think the 'do given' case is useful enough to document
more officially along with 'given'.

If you are thinking about the if/else case,  ?? ... !! would be a
simpler way to write it.

   Mark



could 'given' blocks have a return value?

2006-08-29 Thread Mark Stosberg

Sometimes I use 'given' blocks to set a value. To save repeating myself
on the right hand side of the given block, I found I kept want to do this:

my $foo = given { }

...and have whatever value that was returned from when {} or default {}
populate $foo.

It turns out pugs already allow this, through the trick of wrapping the
given block in an anonymoose sub...which is then immediately executed:

my $rm = sub { given $rm_param {
when Code  { $rm_param(self)   }
when Hash  { %rm_paramrun_mode }
default{ self.query.param($rm_param) }
}}();

Not only do you get implicit matching on the left side, you get implicit
return values on the right!

I'd just like to be able to clean that up a little to:

my $rm = given $rm_param {
when Code  { $rm_param(self)   }
when Hash  { %rm_paramrun_mode }
default{ self.query.param($rm_param) }
};

   Mark



Re: could 'given' blocks have a return value?

2006-08-29 Thread Jonathan Lang

Mark Stosberg wrote:


Sometimes I use 'given' blocks to set a value. To save repeating myself
on the right hand side of the given block, I found I kept want to do this:

my $foo = given { }

...and have whatever value that was returned from when {} or default {}
populate $foo.


Isn't it still the case that the last expression evaluated within a
closure is returned by the closure?  And isn't a given block just a
fancy kind of closure?

The question is whether or not a code block can be used where the
parser expects an expression; for instance, could one say:

 my $foo = if condition {BAR} else {BAZ};

?

I'm no expert, but it occurs to me that allowing this could be a
parsing nightmare.

ISTR a programming construct along the lines of eval that is
effectively shorthand for sub { ... }().


It turns out pugs already allow this, through the trick of wrapping the
given block in an anonymoose sub...which is then immediately executed:

my $rm = sub { given $rm_param {
when Code  { $rm_param(self)   }
when Hash  { %rm_paramrun_mode }
default{ self.query.param($rm_param) }
}}();

Not only do you get implicit matching on the left side, you get implicit
return values on the right!

I'd just like to be able to clean that up a little to:

my $rm = given $rm_param {
when Code  { $rm_param(self)   }
when Hash  { %rm_paramrun_mode }
default{ self.query.param($rm_param) }
};


So what happens if you forget to include a default in the given?

--
Jonathan Dataweaver Lang


Re: could 'given' blocks have a return value?

2006-08-29 Thread Agent Zhang

On 8/30/06, Mark Stosberg [EMAIL PROTECTED] wrote:


Sometimes I use 'given' blocks to set a value. To save repeating myself
on the right hand side of the given block, I found I kept want to do this:

my $foo = given { }



According to S04, given {} is at statement level, so you can't use it
directly as an expression. But Perl 6 always allow you to say

 my $foo = do given {...}

As well as

 my $foo = do if foo {...} else {...}



my $rm = sub { given $rm_param {
when Code  { $rm_param(self)   }
when Hash  { %rm_paramrun_mode }
default{ self.query.param($rm_param) }
}}();



Apparently ``do'' is a simplified version for this. :)

Cheers,
Agent