I think that this is caused because it is returning a 「Sequence」 that is
not getting sunk.
This is because the last value from a function is never sunk in that
function.

You could also use 「eager」 「sink」 or follow it with 「Nil」 or some other
value (instead of 「return」)

    eager $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: (
$a, $b )  });
    sink $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a,
$b )  });
    $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )
 });  Nil
    $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b )
 });  'some other value'

You can also make 「TWEAK」 return a 「Nil」 in the signature (or any
constant/literal).

    submethod TWEAK (-->Nil) { … }

Since the last value in the function is never returned it is also sunk.

---

It might be a good idea if the code that calls 「TWEAK」 (and maybe 「BUILD」)
sinks any result that they get.
Or maybe it should only sink 「Sequence」 values.

There are good reasons for allowing a 「submethod」 to return a value that
doesn't get sunk, so that should not change.

At any rate I don't think there is a bug in how 「submethod TWEAK」 is
compiled and run.
That part is working as intended.

On Sat, Mar 13, 2021 at 2:30 PM <mimosin...@gmail.com> wrote:

> Hi,
>
> When working with this week challenge for the PerlWeeklyChallenge
> <https://perlweeklychallenge.org/>, I noticed this behaviour with TWEAK:
>
> *This does not work:*
>   submethod TWEAK {
>     $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
> )  });
>   }
>
> *This works:*
>   submethod TWEAK {
>     $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
> )  });
>     return;
>   }
>
> It also works with other commands instead of 'return' (like assigning a
> value to a variable). From the examples in the documentation, I am not
> certain this is the expected behaviour.
>
> Thanks for this extraordinary language,
>
> Joan
>
>
> P.S: This is the context where the command appears.  Apologies for the
> messy-Raku,
> ------------------------------
> use Test;
>
> my $data = '1709363,"Les Miserables Episode 1: The Bishop (broadcast date:
> 1937-07-23)"
> 1723781,"Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)"
> 1723781,"Les Miserables Episode 3: The Trial (broadcast date: 1937-08-06)"
> 1678356,"Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)"
> 1646043,"Les Miserables Episode 5: The Grave (broadcast date: 1937-08-20)"
> 1714640,"Les Miserables Episode 6: The Barricade (broadcast date:
> 1937-08-27)"
> 1714640,"Les Miserables Episode 7: Conclusion (broadcast date:
> 1937-09-03)"';
>
> class Movies {
>
>   has $.starttime;
>   has $.currenttime;
>   has $.filelist;
>   has @!show; # ( [ time, show ] )
>
>   submethod TWEAK {
>     # miliseconds -> seconds
>     $!filelist.lines».split(',').map( -> ($a, $b) { @!show.push: ( $a, $b
> )  });
>     return;
>   }
>
>   method what-show() {
>     my $position =  ( $!currenttime - $!starttime ) % @!show[*;0].sum/1000;
>     my ($time, $show);
>     for @!show[*;0] -> $show-time {
>       $time += $show-time;
>       return @!show[$show++;1] if $time > $position;
>     }
>   }
> }
>
> my $mv = Movies.new(
>   starttime   => '1606134123',
>   currenttime => '1614591276',
>   filelist    => $data
> );
>
> is $mv.what-show, '"Les Miserables Episode 1: The Bishop (broadcast date:
> 1937-07-23)"';
>

Reply via email to