我知道Swift已经命名了元组:
let twostraws = (name: "twostraws", password: "fr0st1es")
所以我可以说:
print(twostraws.name) # twostraws
但在Perl 6中,我会说:
my $list = (twostraws, fr0st1es);
say $list[0];
哪一个不像Swift那样出色,所以我想知道Perl 6中是否有命名元组?
最佳答案
有很多类似的方法。
my \twostraws = %( 'name' => 'twostraws', 'password' => 'fr0st1es' );
print twostraws<name>; # twostraws{ qw'name' }
my \twostraws = ( 'twostraws', 'fr0st1es' ) but role {
method name () { self[0] }
method password () { self[1] }
}
put twostraws.name; # `put` is like `print` except it adds a newline
my \twostraws = class :: {
has ($.name, $.password)
}.new( :name('twostraws'), :password('fr0st1es') )
say twostraws.name; # `say` is like `put` but calls the `.gist` method
我可能还没有想到更多。真正的问题是如何在其余的代码中使用它。
关于raku - Perl 6是否已命名元组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36890756/