{新的介绍希望比无用/误导性的原始介绍更好}
@CarlMäsak,在他在这个答案下面写的评论 https://stackoverflow.com/questions/45258965/does-perl-6-have-an-infinite-int#comment83317513_45261924在我的第一个版本之后:
上次我{2014年}与拉里谈过此事 https://irclog.perlgeek.de/perl6/2014-08-20#i_9217249,他的理由似乎是...... Inf 应该适用于所有 Int、Num 和 Str
(我的回答的第一个版本以“回忆”开始,我得出的结论是至少没有帮助,而且似乎是完全错误的记忆。)
在我对卡尔评论的研究中,我确实发现了一个相关的宝石#perl6-dev 于 2016 年 https://irclog.perlgeek.de/perl6-dev/2016-05-21#i_12521585当拉里写道:
那么我们的政策可能是,如果你想要一个支持 ±Inf 和 NaN 的 Int,请使用 Rat
换句话说,不要让 Rat 与 Int 一致,而使其与 Num 一致
拉里写了这篇文章6.c
。我不记得见过任何类似的讨论for 6.d https://github.com/perl6/6.d-prep.
{现在回到我第一个答案的其余部分}
Num
P6 中实现了 IEEE 754 浮点数类型。根据 IEEE 规范,此类型must支持保留代表抽象概念的几个具体值,包括正无穷大的概念。 P6将相应的具体值与术语绑定Inf
.
鉴于这种表示无限的具体值已经存在,它成为一种广泛的语言一般用途对于不涉及浮点数的情况,表示无穷大的具体值,例如在字符串和列表函数中传递无穷大。
我在下面建议的解决你的问题的方法是使用where
条款通过subset
.
A where clause https://docs.perl6.org/type/Signature#index-entry-where_clause_%28Signature%29 allows one to specify run-time assignment/binding "typechecks". I quote "typecheck" because it's the most powerful form of check possible -- it's computationally universal https://en.wikipedia.org/wiki/Turing_completeness and literally checks the actual run-time value (rather than a statically typed view of what that value can be). This means they're slower https://stackoverflow.com/questions/42117027/is-there-research-on-performance-penalties-for-types-constraints-in-perl-6 and run-time, not compile-time, but it also makes them way more powerful (not to mention way easier to express) than even dependent types https://en.wikipedia.org/wiki/Dependent_type which are a relatively cutting edge feature that those who are into advanced statically type-checked languages tend to claim as only available in their own world1 and which are intended to "prevent bugs by allowing extremely expressive types" (but good luck with figuring out how to express them... ;)).
A 子集声明 https://docs.perl6.org/language/typesystem#index-entry-subset-subset可以包括一个where
条款。这可以让您name检查并将其用作命名类型约束。
因此,您可以使用这两个功能来获得您想要的东西:
subset Int-or-Inf where Int:D | Inf;
现在就用那个subset
作为一种类型:
my Int-or-Inf $foo; # ($foo contains `Int-or-Inf` type object)
$foo = 99999999999; # works
$foo = Inf; # works
$foo = Int-or-Inf; # works
$foo = Int; # typecheck failure
$foo = 'a'; # typecheck failure
1. See Does Perl 6 support dependent types? https://stackoverflow.com/questions/38947616/does-perl6-support-dependent-types and .