Perl 6 的 DEFINITE 和 defined 方法有什么区别?
What's the difference between Perl 6's DEFINITE and defined methods?
类型对象总是未定义的,但我见过一些使用 .defined
的测试和一些使用 .DEFINITE
的测试。在任何情况下这些可能会有所不同吗?我倾向于认为任何全大写的方法都不适合日常工作,我更喜欢 .defined
来完成这项任务。
my $class = IntStr;
put '-' x 20; # False
put $class.DEFINITE;
put $class.defined;
put '-' x 20; # False
$class = Nil;
put $class.DEFINITE;
put $class.defined;
put '-' x 20; # True
$class = '';
put $class.DEFINITE;
put $class.defined;
在输出中,我正在寻找两种方法的答案不同的任何情况:
--------------------
False
False
--------------------
False
False
--------------------
True
True
.DEFINITE
应被视为宏(就像 .WHAT
、.HOW
等)。它直接在 Actions 中处理并转换为 nqp::p6definite()
op.
.defined
是一种存在于 Mu
中的方法,可以被您的 class 覆盖。它实际上被 Failure
覆盖,因此实例化的 Failure
可以充当未定义的值,例如if
语句(和 "handle" 失败)。
my $a = Failure.new("foo");
say "bar" if $a; # no output
say $a; # outputs "(HANDLED) foo", but no longer throws
所以回答你的问题:
my $a = Failure.new("foo");
say $a.DEFINITE; # True
say $a.defined; # False
类型对象总是未定义的,但我见过一些使用 .defined
的测试和一些使用 .DEFINITE
的测试。在任何情况下这些可能会有所不同吗?我倾向于认为任何全大写的方法都不适合日常工作,我更喜欢 .defined
来完成这项任务。
my $class = IntStr;
put '-' x 20; # False
put $class.DEFINITE;
put $class.defined;
put '-' x 20; # False
$class = Nil;
put $class.DEFINITE;
put $class.defined;
put '-' x 20; # True
$class = '';
put $class.DEFINITE;
put $class.defined;
在输出中,我正在寻找两种方法的答案不同的任何情况:
--------------------
False
False
--------------------
False
False
--------------------
True
True
.DEFINITE
应被视为宏(就像 .WHAT
、.HOW
等)。它直接在 Actions 中处理并转换为 nqp::p6definite()
op.
.defined
是一种存在于 Mu
中的方法,可以被您的 class 覆盖。它实际上被 Failure
覆盖,因此实例化的 Failure
可以充当未定义的值,例如if
语句(和 "handle" 失败)。
my $a = Failure.new("foo");
say "bar" if $a; # no output
say $a; # outputs "(HANDLED) foo", but no longer throws
所以回答你的问题:
my $a = Failure.new("foo");
say $a.DEFINITE; # True
say $a.defined; # False