Perl 6 添加對(duì)象散列, 其鍵并不僅僅是字符串。這些鍵是值和類型的結(jié)合闹炉。這意味著對(duì)象可以被字符串化為同樣的東西但是它們可以是不同的鍵蒿赢。
普通的哈希構(gòu)造:
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;
結(jié)果顯示該哈希中只有一個(gè)元素并且這個(gè)元素是我最后添加的那個(gè):
There are 1 elements in the hash
{4 => Dualvar}
但是我也可以通過告訴哈希我想要它接受的對(duì)象來聲明一個(gè)對(duì)象哈希(Object hash)。我可以使用 Any
對(duì)象來允許哈希接受任何東西:
my %hash{Any}; # accept any sort of object
下面的程序幾乎和上面的一樣渣触,但是表現(xiàn)的很不同:
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;
現(xiàn)在我能在該哈希中看到 3 個(gè)元素了诉植。這個(gè)哈希以 .gist
形式打印出來后看起來有點(diǎn)奇怪,因?yàn)樗?4 個(gè)鍵都是 4:
There are 3 elements in the hash
{4 => Dualvar, 4 => Plain old number, 4 => String of digits}
使用 .perl
方法能看到背后的真相:
%hash.perl.say;
現(xiàn)在我能看到該哈希中有 3 種不同的對(duì)象:
There are 3 elements in the hash
(my Any %{Any} = IntStr.new(4, "4") => "Dualvar", 4 => "Plain old number", "4" => "String of digits")
用上對(duì)象哈希后昵观,測(cè)試存在性就有點(diǎn)不同了晾腔。它使用 .WHICH
方法,使用對(duì)象相等操作符 ===
來比較鍵啊犬。
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
看起來像同一個(gè)對(duì)象灼擂。然而,鍵 "4" 不在該哈希中即使它擁有同樣的字符串 "4":
int: Int|4
other: Int|4
Same object
Other int exists in hash
"4" not there
如果它和我的期望不一樣這看起來就可能有點(diǎn)奇怪觉至。
我們來看尖括號(hào)版本的引號(hào)單詞操作符剔应,<...>
。這種形式的引號(hào)單詞創(chuàng)建了 allomorphs(字素變體)。當(dāng)它看見像數(shù)字那樣的東西時(shí)峻贮,它創(chuàng)建繼承自數(shù)字和字符串兩邊的諸如 IntStr
的東西席怪。這意味著,盡管它作為一個(gè)對(duì)象哈希纤控,但是它擁有一個(gè)很特殊的形式挂捻。在下面這個(gè)哈希對(duì)象中,我使用 <>
引號(hào)在鍵 4 的周圍創(chuàng)建了一個(gè)元素船万。然后我測(cè)試字符串 "4" 是否在該哈希中:
use v6;
my %hash{Any};
%hash = 1;
say %hash{"4"}:exists ?? 'Exists in hash' !! 'Not there';
我看到它并沒有在該哈希中:
Not there
這個(gè)語素變體版本是一個(gè) IntStr, 其中 "4" 是一個(gè) Str刻撒。它們不是同一個(gè)對(duì)象,所以后者不是該哈希中的鍵耿导。
如果這正是你所期待的声怔,這不是一個(gè)大問題。但是舱呻,考慮一個(gè)更有用的只允許某種類型的對(duì)象的對(duì)象哈希演怎。也許我想讓它們都是 Date
類型的對(duì)象份名。下面這種方式不能工作:
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 };
當(dāng)我試圖繞過約束的時(shí)候贩据,我得到一個(gè)異常:
Type check failed in binding to key; expected Date but got Str ("12-03-2016")
Perl 6 讓我強(qiáng)迫其他程序員按照我需要的方式構(gòu)造哈希鍵贫贝。
最后 Zoffix Znet 補(bǔ)充了一句:
你還可以約束值的類型。申明變量的時(shí)候把類型放在變量名前面就好了:
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)