对象哈希

第三天 - 对象哈希

Raku 添加对象散列, 其键并不仅仅是字符串。这些键是值和类型的结合。这意味着对象可以被字符串化为同样的东西但是它们可以是不同的键。

普通的哈希构造:

use v6;

my Int    $int     = 4;
my Str    $str     = "4";
my IntStr $int_str = <4>;  # Allomorph

my %hash;
%hash{$int}     = 'Plain old number';
%hash{$str}     = 'String of digits';
%hash{$int_str} = 'Dualvar';

say "There are ", %hash.elems, " elements in the hash";

# this calls the .gist method, sorta like a dumper routine
%hash.say;

结果显示该哈希中只有一个元素并且这个元素是我最后添加的那个:

There are 1 elements in the hash
{4 => Dualvar}

但是我也可以通过告诉哈希我想要它接受的对象来声明一个对象哈希(Object hash)。我可以使用 Any 对象来允许哈希接受任何东西:

my %hash{Any}; # accept any sort of object

下面的程序几乎和上面的一样,但是表现的很不同:

use v6;

my Int    $int     = 4;
my Str    $str     = "4";
my IntStr $int_str = <4>;  # Allomorph

my %hash{Any};
%hash{$int}     = 'Plain old number';
%hash{$str}     = 'String of digits';
%hash{$int_str} = 'Dualvar';

say "There are ", %hash.elems, " elements in the hash";

# this calls the .gist method, sorta like a dumper routine
%hash.say;

现在我能在该哈希中看到 3 个元素了。这个哈希以 .gist 形式打印出来后看起来有点奇怪,因为它有 4 个键都是 4:

There are 3 elements in the hash
{4 => Dualvar, 4 => Plain old number, 4 => String of digits}

使用 .perl 方法能看到背后的真相:

%hash.perl.say;

现在我能看到该哈希中有 3 种不同的对象:

There are 3 elements in the hash
(my Any %{Any} = IntStr.new(4, "4") => "Dualvar", 4 => "Plain old number", "4" => "String of digits")

用上对象哈希后,测试存在性就有点不同了。它使用 .WHICH 方法,使用对象相等操作符 === 来比较键。

use v6;

my Int    $int     = 4;
my IntStr $int_str = <4>;  # Allomorph

my %hash{Any};
%hash{$int}     = 'Plain old number';
%hash{$int_str} = 'Dualvar';

my $other_int = 4;

# what are these things?
say "int: " ~ $int.WHICH;
say "other: " ~ $other_int.WHICH;

# are they the same?
say $int === $other_int ?? 'Same object' !! 'Different object';

# is it in the hash?
say %hash{$other_int}:exists ?? 'Other int exists in hash' !! 'Other int not there';

say %hash{"4"}:exists ?? '"4" exists in hash' !! '"4" not there';

我可以看到 $int$other_int 看起来像同一个对象。然而,键 “4” 不在该哈希中即使它拥有同样的字符串 “4”:

int: Int|4
other: Int|4
Same object
Other int exists in hash
"4" not there

如果它和我的期望不一样这看起来就可能有点奇怪。

我们来看尖括号版本的引号单词操作符,<...>。这种形式的引号单词创建了 allomorphs(字素变体)。当它看见像数字那样的东西时,它创建继承自数字和字符串两边的诸如 IntStr 的东西。这意味着,尽管它作为一个对象哈希,但是它拥有一个很特殊的形式。在下面这个哈希对象中,我使用 <> 引号在键 4 的周围创建了一个元素。然后我测试字符串 “4” 是否在该哈希中:

use v6;

my %hash{Any};

%hash = 1;

say %hash{"4"}:exists ?? 'Exists in hash' !! 'Not there';

我看到它并没有在该哈希中:

Not there

这个语素变体版本是一个 IntStr, 其中 “4” 是一个 Str。它们不是同一个对象,所以后者不是该哈希中的键。

如果这正是你所期待的,这不是一个大问题。但是,考虑一个更有用的只允许某种类型的对象的对象哈希。也许我想让它们都是 Date 类型的对象。下面这种方式不能工作:

my %hash{Date};
%hash{Date.new(now)} =  ( event => 'Something cool', rating => '6 stars' );

my $too_cool_for_your_api = '12-03-2016';
say %hash{ $too_cool_for_your_api };

当我试图绕过约束的时候,我得到一个异常:

Type check failed in binding to key; expected Date but got Str ("12-03-2016")

Raku 让我强迫其他程序员按照我需要的方式构造哈希键。

最后 Zoffix Znet 补充了一句:

你还可以约束值的类型。申明变量的时候把类型放在变量名前面就好了:

my Date %hash{Int}; # use Int keys and accept only Date values

%hash{42} = 72; # Type check failed in binding to assignval; expected Date but got Int (72)
comments powered by Disqus