Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrayref and hashref destructuring in subroutine signatures and assignment #55

Open
akarelas opened this issue Oct 4, 2024 · 1 comment

Comments

@akarelas
Copy link

akarelas commented Oct 4, 2024

It would be nice if we could do:

sub foo ([$x, $y]) {
    say "$x, $y";
}

...instead of:

sub foo ($pair) {
    my ($x, $y) = @$pair;
    say "$x, $y";
}

...but also (if the previous has been done):

my { $x, $y } = { x => 5, y => 6 };

...or:

my { x => $value_x, y => $value_y } = { x => 5, y => 6 };

Would certainly help with users of RxPerl, who often write code like:

$observable->pipe(
    op_pairwise,
    op_map(sub ($pair) {
        my ($old, $new) = @$pair;
        ...
    }),
);
@guest20
Copy link

guest20 commented Oct 4, 2024

I like that the my { x => $value_x, y => $value_y } = { x => 5, y => 6 } de-structuring looks like regular structuring.

It almost feels like I should be able to put any reference constructor on the my side and have any variable referenced there get lvalue'ed up:

my { advanced => { destructuring => [undef, undef, $third_value] } } = $deep_struct;

How far can this go?

Can it express de-structure into a lexical sub or the symbol table?
Maybe even capturing a method call on an object as a code-reference my \&callback = \&$obj->method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants