Perl 6中的任何类型的列表是什么类型? [英] What type are Lists of any type in Perl 6?

查看:71
本文介绍了Perl 6中的任何类型的列表是什么类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下Python代码(作为示例):

Consider the following Python code (as an example):

a = 5 
b = "a"
l = [a, b] # -> typing.List[typing.Any]
print(l)   
# [5, "a"]

列表l的类型为 list ;它不受它所拥有的类型的限制,因为Python是完全动态键入的.

The type of list l is list; it is not constrained by the types it holds because Python is quite dynamically typed.

将其与Go(在结构上类型强的类型)进行对比:

Contrast this with, say, Go, which is strongly structurally typed:

var list []uint8{1, 2, 3, 4, 5, 6}

该列表只能容纳最多255个无符号整数.它不能容纳任何其他类型.

That list can only hold unsigned integers up to 255. It cannot hold any other type.

也可以:

var multi interface{"string", []int{9, 5}, rune('5'), []interface{}}

接口允许使用变体类型的容器.

Interfaces allow for containers of variant types.

考虑Perl 6,它比Python更动态地键入,因为say 6 + "1";将给出7,即整数. (谁以为我不知道这是个好主意.)

Consider Perl 6, which is even more dynamically typed than Python in that say 6 + "1"; will give 7, the integer. (Who thought this was a good idea I don't know.)

我喜欢在程序中进行渐进式输入,因为它(尤其是我正在学习的Perl 6)可以提高可读性和可维护性.

I like gradual typing in my programs as (especially for Perl 6, which I'm learning) it improves readability and maintanability.

以下两项均不可用:

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>)给出相同的错误).

You get Type check failed in assignment to @l; expected List but got Int. (Other list construction syntaxes ([vals], <vals>) give the same error).

所做的工作是说类型为Any(或Mu),这很有意义. (嗯,这对我来说很有意义,因为Any是Python 3.5使用的相同关键字.)

What does work is saying the type is Any (or Mu) which makes sense. (Well, it makes sense to me because Any's the same keyword Python 3.5 uses.)

use strict;    
my Int $n = 6;
my Str $x = "a";
my Any @l = $n, $x;

但是使用AnyMu有点首先破坏了类型检查的目的.

But using Any or Mu kinda defeats the purpose of type checking in the first place.

如果不是List,列表的类型是什么?此外,如果类型检查永远不会通过任何值或其列表,那么my List $blah;为什么是有效的语法?

What's the type of a list, if it's not List? Moreover, why is my List $blah; valid syntax if the type check will never pass for any value or List thereof?

推荐答案

您解释错了,至少Python中的 list 与Perl6中的 list 不同就像Perl6中的数组(而Perl6 list 就像Python tuple ).

You interpreted it wrong, at least list in Python is different from list in Perl6, it's actually like array in Perl6 (and Perl6 list is like Python tuple).

当您这样做:

my List @l = $n, $n;

您创建数组@l 第二个例子:

use strict;
my Int $n = 6;
my Int @l = $n, $n;

必须工作.

代码:

my Any @l = $n, $x;

与以下相同:

my @l = $n, $x;

您已允许数组@l的元素为任何类型.

You have allowed the elements of array @l to be any type.

这篇关于Perl 6中的任何类型的列表是什么类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆