Looks like I can get pretty close to what I was looking for with this
subroutine:

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

Then, given the Point class again:

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

this:

    (Point.new.&cascade: {.x = 10;}; Point.new.&cascade: {.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) { proc($obj); $obj; }

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

    my $app = $_;

    my $button;

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

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

    .border-width = 20;

    .run;
};



On Mon, Jun 6, 2016 at 1:12 PM, Eduardo Cavazos <wayo.cava...@gmail.com>
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
>

Reply via email to