.Bool, .so, 之间的区别?所以 [英] Differences between .Bool, .so, ? and so
问题描述
我想弄清楚上述例程和 if 语句之间的区别是什么
I’m trying to figure out what the differences are between the above-mentioned routines, and if statements like
say $y.Bool;
say $y.so;
say ? $y;
say so $y;
会产生不同的结果.
到目前为止,对我来说唯一明显的区别是 ?
比 so
具有更高的优先级..Bool
和 .so
似乎完全是同义词.这是正确的吗?(实际上)是完整的故事吗?
So far the only difference that is apparent to me is that ?
has a higher precedence than so
. .Bool
and .so
seem to be completely synonymous. Is that correct and (practically speaking) the full story?
推荐答案
为了回答你的问题,我所做的是解析 Rakudo 编译器源代码.
What I've done to answer your question is to spelunk the Rakudo compiler source code.
正如您所注意到的,前缀之间不同的一个方面是解析差异.这些变体具有不同的优先级,so
是字母,而 ?
是标点符号.要查看控制此解析的精确代码,请查看 Rakudo 的 Grammar.nqp
并在该页面中搜索 prefix:sym<...>
其中 ...
是 ?
、so
等.它看起来像 三元 (... ?? ... !! ...
) 变成了 if
.我看到这些令牌都没有相应地命名 Actions.pm6
方法.作为一个有点疯狂的猜测,与它们对应的代码生成可能由 这部分method EXPR
.(任何人都知道或愿意按照这篇博文 找出答案?)
As you note, one aspect that differs between the prefixes is parsing differences. The variations have different precedences and so
is alphabetic whereas ?
is punctuation. To see the precise code controlling this parsing, view Rakudo's Grammar.nqp
and search within that page for prefix:sym<...>
where the ...
is ?
, so
, etc. It looks like ternary (... ?? ... !! ...
) turns into an if
. I see that none of these tokens have correspondingly named Actions.pm6
methods. As a somewhat wild guess perhaps the code generation that corresponds to them is handled by this part of method EXPR
. (Anyone know, or care to follow the instructions in this blog post to find out?)
代码中的定义.pm6 和 Mu.pm6
表明:
在
Mu.pm6
中,方法.Bool
为未定义的对象返回False
和.defined
> 否则.反过来,.defined
为未定义的对象返回False
,否则返回True
.所以这些是默认的.
In
Mu.pm6
the method.Bool
returnsFalse
for an undefined object and.defined
otherwise. In turn.defined
returnsFalse
for an undefined object andTrue
otherwise. So these are the default.
.defined
被记录为在两个内置类中被覆盖 和 .Bool
19.
.defined
is documented as overridden in two built in classes and .Bool
in 19.
so
、.so
和 ?
都调用遵循 Bool
的相同代码/.Bool
.理论上类/模块可以覆盖这些而不是,或者也覆盖 .Bool
或 .defined
,但我不明白为什么有人会这样做在内置类/模块或用户空间中.
so
, .so
, and ?
all call the same code that defers to Bool
/ .Bool
. In theory classes/modules could override these instead of, or as well, as overriding .Bool
or .defined
, but I can't see why anyone would ever do that either in the built in classes/modules or userland ones.
not
和 !
是一样的(除了 使用 !
和 :exists
dies) 并且都变成调用到 nqp::hllbool(nqp::not_i(nqp::istrue(...)))
.我认为他们不通过通常的 .Bool
路线的主要原因是为了避免 标记处理Failure
s.
not
and !
are the same (except that use of !
with :exists
dies) and both turn into calls to nqp::hllbool(nqp::not_i(nqp::istrue(...)))
. I presume the primary reason they don't go through the usual .Bool
route is to avoid marking handling of Failure
s.
在Mu.pm6
中定义了.so
和.not
方法.他们只是调用 .Bool
.
There are .so
and .not
methods defined in Mu.pm6
. They just call .Bool
.
有包含 ?
的布尔按位运算符.他们与您的问题相去甚远,但他们的代码包含在上面的链接中.
There are boolean bitwise operators that include a ?
. They are far adrift from your question but their code is included in the links above.
这篇关于.Bool, .so, 之间的区别?所以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!