Perl 6 Calendar 2016 - 第三天?。?duì)象哈希

第三天 - 對(duì)象哈希

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ì)象來(lái)聲明一個(gè)對(duì)象哈希(Object hash)。我可以使用 Any 對(duì)象來(lái)允許哈希接受任何東西:

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 形式打印出來(lái)后看起來(lái)有點(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ì)象相等操作符 === 來(lái)比較鍵。

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 看起來(lái)像同一個(gè)對(duì)象。然而,鍵 "4" 不在該哈希中即使它擁有同樣的字符串 "4":

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

如果它和我的期望不一樣這看起來(lái)就可能有點(diǎn)奇怪。

我們來(lái)看尖括號(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è)語(yǔ)素變體版本是一個(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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 標(biāo)題: Rakudo and NQP Internals子標(biāo)題: The guts tormented imple...
    焉知非魚閱讀 1,523評(píng)論 1 3
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,706評(píng)論 18 399
  • 從匹配中返回值 Match 對(duì)象 成功的匹配總是返回一個(gè) Match 對(duì)象, 這個(gè)對(duì)象通常也被放進(jìn) $/ 中, (...
    焉知非魚閱讀 1,937評(píng)論 0 1
  • 今天跟媽媽說了,嗯嗯感情公開了,要好好的處了。王逗比,如果你對(duì)我不好,我就去找未婚夫去~哼~
    惠木子小姐閱讀 183評(píng)論 0 1
  • 每天:3.5h 每周:3.5x5=17.5h 每月:17.5x4=70h 每年:70x12=840h 840h=5...
    暢談一下閱讀 577評(píng)論 2 2

友情鏈接更多精彩內(nèi)容