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

delegate Array/Hash methods in a consistent way #5362

Open
p6rt opened this issue Jun 6, 2016 · 1 comment
Open

delegate Array/Hash methods in a consistent way #5362

p6rt opened this issue Jun 6, 2016 · 1 comment
Labels
RFC Request For Comments

Comments

@p6rt
Copy link

p6rt commented Jun 6, 2016

Migrated from rt.perl.org#128330 (status was 'new')

Searchable as RT128330$

@p6rt
Copy link
Author

p6rt commented Jun 6, 2016

From @LLFourn

Delegating array behaviour to an attribute is a common pattern.

class Foo {
  has @​!interal handles <AT-POS EXISTS-POS elems end iterator>
}

But the above is easy to get wrong. And is wrong. It doesn't work for [*-1]
like access because internally rakudo is using .cache.elems.

'class Foo does Positional { has $.val handles <AT-POS EXISTS-POS elems end
iterator>; }; my $f = Foo.new(val => [5,6]); say $f[1], $f[*-1]' # 5,6
(should be 6,6)

This RT is to suggest that there should be some way to say delegate "all
the things that would make &postcircumfix​:sym<[ ]> work" (and likewise for
{ }) and perhaps other things. These "delegation groups" would be
implementation independent, ie different Perl 6 implementations could
choose to delegate different sets of methods to get [] to work properly.

Of course the right way to do this most of the time is to do class Foo is
Array { ... } which works fine. But sometimes has-a relationships are
preferable to is-a.

So the questions to be answered are​:

1. is this a good idea?
2. how do we do this using the pre-existing "handles" trait syntax? (roles
with lists of methods maybe?)
3. how can we divide the groups of methods so you can opt-in only to
particular behaviour? ie you may want to delegate [] stuff like AT-POS but
not have <push pop shift unshift append prepend> (or vis versa or both).

@p6rt p6rt added the RFC Request For Comments label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request For Comments
Projects
None yet
Development

No branches or pull requests

1 participant