Perl 6 中任何类型的列表是什么类型?
What type are Lists of any type in Perl 6?
考虑以下 Python 代码(作为示例):
a = 5
b = "a"
l = [a, b] # -> typing.List[typing.Any]
print(l)
# [5, "a"]
列表l
的类型是list
;它不受其持有的类型的限制,因为 Python 是动态类型的。
将其与强结构类型的 Go 进行对比:
var list []uint8{1, 2, 3, 4, 5, 6}
该列表最多只能容纳 255 个无符号整数。它不能容纳任何其他类型。
也去:
var multi interface{"string", []int{9, 5}, rune('5'), []interface{}}
接口允许使用变体类型的容器。
考虑 Perl 6,它比 Python 的类型更动态,因为 say 6 + "1";
将给出 7
整数。 (我不知道谁认为这是个好主意。)
我喜欢在我的程序中逐渐输入(尤其是对于我正在学习的 Perl 6)它提高了可读性和可维护性。
以下都不是:
use strict;
my Int $n = 6;
my Str $x = "a";
my Int @l = $n, $x;
也不
use strict;
my Int $n = 6;
my Str $x = "a";
my List @l = $n, $x;
你得到 Type check failed in assignment to @l; expected List but got Int
。 (其他列表构造语法([vals]
、<vals>
)给出相同的错误)。
的作用是说类型是 Any
(或 Mu
),这是有道理的。 (好吧,这对我来说很有意义,因为 Any
与 Python 3.5 使用的关键字相同。)
use strict;
my Int $n = 6;
my Str $x = "a";
my Any @l = $n, $x;
但是使用 Any
或 Mu
有点违背了类型检查的初衷。
列表的类型是什么,如果不是 List
?此外,如果类型检查永远不会通过任何值或其列表,为什么 my List $blah;
有效语法?
你理解错了,至少Python中的list和Perl6中的list是不一样的,它其实是数组在 Perl6 中(Perl6 list 就像 Python tuple)。
当你这样做时:
my List @l = $n, $n;
you create an array @l
and enforce all its elements must be type List
.
所以你的第二个例子:
use strict;
my Int $n = 6;
my Int @l = $n, $n;
必须工作。
代码:
my Any @l = $n, $x;
等同于:
my @l = $n, $x;
您已允许数组 @l
的元素为任何类型。
如果您希望列表的元素是 type-checked,那么您可以使用绑定 :=
而不是赋值 =
:
my Int $n = 6;
my Str $x = "a";
# Bind $n and $x to elements of @bound
my @bound := $n, $x;
@bound[1].say; # Prints "a"
@bound[1] = "Hello"; # Changing the second element changes $x
$x.say; # Prints "Hello"
@bound[1] = 6; # Dies with error "Type check failed in assignment to $x; expected Str but got Int"
请注意,最后一行产生的错误是指 $x
,而不是 @bound[1]
。
考虑以下 Python 代码(作为示例):
a = 5
b = "a"
l = [a, b] # -> typing.List[typing.Any]
print(l)
# [5, "a"]
列表l
的类型是list
;它不受其持有的类型的限制,因为 Python 是动态类型的。
将其与强结构类型的 Go 进行对比:
var list []uint8{1, 2, 3, 4, 5, 6}
该列表最多只能容纳 255 个无符号整数。它不能容纳任何其他类型。
也去:
var multi interface{"string", []int{9, 5}, rune('5'), []interface{}}
接口允许使用变体类型的容器。
考虑 Perl 6,它比 Python 的类型更动态,因为 say 6 + "1";
将给出 7
整数。 (我不知道谁认为这是个好主意。)
我喜欢在我的程序中逐渐输入(尤其是对于我正在学习的 Perl 6)它提高了可读性和可维护性。
以下都不是:
use strict;
my Int $n = 6;
my Str $x = "a";
my Int @l = $n, $x;
也不
use strict;
my Int $n = 6;
my Str $x = "a";
my List @l = $n, $x;
你得到 Type check failed in assignment to @l; expected List but got Int
。 (其他列表构造语法([vals]
、<vals>
)给出相同的错误)。
的作用是说类型是 Any
(或 Mu
),这是有道理的。 (好吧,这对我来说很有意义,因为 Any
与 Python 3.5 使用的关键字相同。)
use strict;
my Int $n = 6;
my Str $x = "a";
my Any @l = $n, $x;
但是使用 Any
或 Mu
有点违背了类型检查的初衷。
列表的类型是什么,如果不是 List
?此外,如果类型检查永远不会通过任何值或其列表,为什么 my List $blah;
有效语法?
你理解错了,至少Python中的list和Perl6中的list是不一样的,它其实是数组在 Perl6 中(Perl6 list 就像 Python tuple)。
当你这样做时:
my List @l = $n, $n;
you create an array @l
and enforce all its elements must be type List
.
所以你的第二个例子:
use strict;
my Int $n = 6;
my Int @l = $n, $n;
必须工作。
代码:
my Any @l = $n, $x;
等同于:
my @l = $n, $x;
您已允许数组 @l
的元素为任何类型。
如果您希望列表的元素是 type-checked,那么您可以使用绑定 :=
而不是赋值 =
:
my Int $n = 6;
my Str $x = "a";
# Bind $n and $x to elements of @bound
my @bound := $n, $x;
@bound[1].say; # Prints "a"
@bound[1] = "Hello"; # Changing the second element changes $x
$x.say; # Prints "Hello"
@bound[1] = 6; # Dies with error "Type check failed in assignment to $x; expected Str but got Int"
请注意,最后一行产生的错误是指 $x
,而不是 @bound[1]
。