Re: Variant of 'with' which returns target object

2016-06-06 Thread yary
On Mon, Jun 6, 2016 at 5:47 PM, yary  wrote:

> GTK::Simple::Button.new(label => 'bcd').clicked. ...


whoops, that should be

with GTK::Simple::Button.new(label => 'bcd') {
 .clicked.tap({ .sensitive = False; $button.sensitive =
True; });
}

alas I am on windows and Panda can't install GTK::Simple on my machine, not
able to test.

-y


Re: Variant of 'with' which returns target object

2016-06-06 Thread yary
That's a nice & small answer. It does seem overkill for the gtk example...

use v6;

use GTK::Simple;

with GTK::Simple::App.new(title => 'abc') {

my $app = $_;

my $button;

.set-content:
GTK::Simple::VBox.new(
GTK::Simple::Button.new(label => 'bcd').clicked.tap({
.sensitive = False; $button.sensitive = True; });

   $button = GTK::Simple::Button.new(label => 'cde');
   $button.clicked.tap({ $app.exit; });
);

.border-width = 20;

.run;
};


Re: Variant of 'with' which returns target object

2016-06-06 Thread Eduardo Cavazos
Looks like I can get pretty close to what I was looking for with this
subroutine:

sub cascade ($obj, ) { proc($obj); $obj; }

Then, given the Point class again:

class Point { has $.x is rw; has $.y is rw; }

this:

(Point.new.: {.x = 10;}; Point.new.: {.y = 20;})

returns:

(Point.new(x => 10, y => Any) Point.new(x => Any, y => 20))

This came out of looking to see if it was possible to support method
cascade syntax in Perl 6. See this question on stackoverflow:

http://stackoverflow.com/questions/37559870/method-cascade-syntax

Below, I've included the gtk-simple example written to use 'cascade'.

As an aside, the Swift community has been discussing adding something like
'cascade' to their standard library under the name 'with':

https://gist.github.com/erica/96d9c5bb4eaa3ed3b2ff82dc35aa8dae

Ed

use v6;

use GTK::Simple;

sub cascade ($obj, ) { proc($obj); $obj; }

GTK::Simple::App.new(title => 'abc').: {

my $app = $_;

my $button;

.set-content:
GTK::Simple::VBox.new(
GTK::Simple::Button.new(label => 'bcd').: {
.clicked.tap({ .sensitive = False; $button.sensitive =
True; });
   };

   $button = GTK::Simple::Button.new(label => 'cde').: {
   .clicked.tap({ $app.exit; });
   }
);

.border-width = 20;

.run;
};



On Mon, Jun 6, 2016 at 1:12 PM, Eduardo Cavazos 
wrote:

> Hello,
>
> Here's a simple class:
>
> class Point { has $.x is rw; has $.y is rw; }
>
> 'with' seems to return the result of evaluating the block. E.g. this
> expression will return a Point:
>
> with Point.new { .x = 10; $_; }
>
> Whereas this will return 10:
>
> with Point.new { .x = 10; }
>
> Is there a way for a user to define a variant of 'with' which returns the
> target object instead of the result of the evaluating the block? I.e.:
>
> with_alt Point.new { .x = 10; }
>
> would return a Point.
>
> And this:
>
> (with_alt Point.new {.x = 10}; with_alt Point.new {.y = 20})
>
> would return a list of two Points.
>
> Thanks!
>
> Ed
>


Re: "with" definedness check

2016-06-06 Thread yary
On Mon, Jun 6, 2016 at 3:47 PM, Brandon Allbery  wrote:
> But for that there is "given". I thought the whole point of "with" vs.
> "given" was the definedness check.

Ah yes, and that's a great feature. I forgot that "with" skips over
the block when the topic is undefined, and that is very useful. I just
had some not-great examples in my REPL, and had forgotten it.

-y


Re: "with" definedness check

2016-06-06 Thread Brandon Allbery
On Mon, Jun 6, 2016 at 3:45 PM, yary  wrote:

> On Mon, Jun 6, 2016 at 3:27 PM, Elizabeth Mattijsen 
> wrote:
> > “with” is completely agnostic about what it is working on.  It merely
> checks for definedness and sets the topicalizer if so.
>
> Hmm- what's the  benefit of with's defined check? Seems like it makes
> "with" break if used with type objects.
>

But for that there is "given". I thought the whole point of "with" vs.
"given" was the definedness check.

-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Re: Variant of 'with' which returns target object

2016-06-06 Thread Eduardo Cavazos
On Mon, Jun 6, 2016 at 2:08 PM, yary  wrote:

>
> For your particular case, would it be sufficient to set the values in
> the constructor?
>
> with Point.new( :x(10) ) { .say }
>   # says "Point.new(x => 10, y => Any)"
>

Normally, that would be a good way to construct the Points in my example.
I'm just using the approach in my note to demonstrate 'with'.

Ed


Re: Variant of 'with' which returns target object

2016-06-06 Thread yary
On Mon, Jun 6, 2016 at 2:12 PM, Eduardo Cavazos  wrote:
>
> with_alt Point.new { .x = 10; }
>
> would return a Point.
>
> And this:
>
> (with_alt Point.new {.x = 10}; with_alt Point.new {.y = 20})

For your particular case, would it be sufficient to set the values in
the constructor?

with Point.new( :x(10) ) { .say }
  # says "Point.new(x => 10, y => Any)"

with Point.new( :x(10) ), Point.new( :y(20) ) { .perl }
  # says "(Point.new(x => 10, y => Any), Point.new(x => Any, y => 20))"

It's not the general solution, but it does the trick for the examples.

-y


Variant of 'with' which returns target object

2016-06-06 Thread Eduardo Cavazos
Hello,

Here's a simple class:

class Point { has $.x is rw; has $.y is rw; }

'with' seems to return the result of evaluating the block. E.g. this
expression will return a Point:

with Point.new { .x = 10; $_; }

Whereas this will return 10:

with Point.new { .x = 10; }

Is there a way for a user to define a variant of 'with' which returns the
target object instead of the result of the evaluating the block? I.e.:

with_alt Point.new { .x = 10; }

would return a Point.

And this:

(with_alt Point.new {.x = 10}; with_alt Point.new {.y = 20})

would return a list of two Points.

Thanks!

Ed