哈希的代理对象?

问题描述 投票:4回答:1

如何为哈希创建代理对象?我似乎找不到传递哈希键的方法:

#sub attr() is rw {
sub attr($name) is rw {
  my %hash;
  Proxy.new(
    FETCH => method (Str $name) { %hash«$name» },
    STORE => method (Str $name, $value) { %hash«$name» = $value }
  );
}

my $attr := attr();
$attr.bar = 'baz';
say $attr.bar;
object hash proxy perl6
1个回答
2
投票

A Proxy代替了单个容器ScalarScalar是多个容器,其中每个元素默认都是Hash

一种可能的解决方案(基于Scalar)是将How to add subscripts to my custom Class in Perl 6?的实现委派给内部哈希,但重写Associative方法以将默认的AT-KEY替换为Scalar

Proxy
© www.soinside.com 2019 - 2024. All rights reserved.