本文介绍了Perl 6 的 DEFINITE 和定义的方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类型对象总是未定义的,但我见过一些使用 .defined 的测试和一些使用 .DEFINITE 的测试.有没有可能不同的情况?我倾向于认为任何全大写的方法都不适合日常工作,并且更喜欢 .defined 来完成这项任务.

Type objects are always undefined, but I've seen some tests that use .defined and some that use .DEFINITE. Is there any case where those might be different? I tend to think that any method that's all uppercase isn't for everyday work and would prefer .defined for this task.

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;

在输出中,我正在寻找两种方法的答案不同的任何情况:

In the output I'm looking for any case where the answers to the two methods would be different:

--------------------
False
False
--------------------
False
False
--------------------
True
True

推荐答案

.DEFINITE 应该被认为是一个宏(就像 .WHAT, .HOW 等).它在 Actions 中直接处理并转换为 nqp::p6define() op.

.DEFINITE should be considered a Macro (just like .WHAT, .HOW etc). It is directly handled in the Actions and converted to a nqp::p6definite() op.

.defined 是一个存在于 Mu 中的方法,它可以被你的类覆盖.它实际上被 Failure 覆盖,因此实例化的 Failure 可以充当未定义的值,例如if 语句(并处理"失败).

.defined is a method that lives in Mu and which can be overridden by your class. It is in fact overridden for Failure, so that an instantiated Failure can act as a undefined value in e.g. an if statement (and "handle" the Failure).

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

这篇关于Perl 6 的 DEFINITE 和定义的方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 23:40