Raku 中 (Any) 的含义是什么 - 特别是 ()? [英] What is the meaning of (Any) in Raku - specifically the ()?
问题描述
这是 Raku 的实验:
Here is an experiment with Raku:
> my $x
(Any)
> my $y=1
1
> my @a=[1, 2]
[1 2]
> my %h=a=>'b'
{a => b}
> say "nil" unless $x
nil
我可以看到 []
表示数组文字,{}
表示哈希文字.
I can see that []
indicates an array literal, {}
a hash literal.
我还可以看到 (Any)
的行为类似于 nil - 在上面显示的布尔上下文中返回 false.
I can also see that (Any)
behaves like nil - returning false in the boolean context shown above.
我觉得 (Any)
很有趣.文档告诉我 Any
只是 Raku 中的神类之一.但是 Any
周围的括号 ()
告诉我什么?
I find the (Any)
interesting. The documentation tells me that Any
is just one of the god classes in Raku. But what do the parenthesis ()
around Any
tell me?
推荐答案
当您使用 REPL 时,使用 say
显示表达式的结果.say
函数调用表达式上的 .gist
函数.
When you use the REPL, the result of the expression is shown using say
. The say
function calls the .gist
function on the expression.
Any
是一个类型对象.类型对象有一个 .gist
方法,将括号放在它们周围.
Any
is a type-object. Type-objects have a .gist
method that puts the parentheses around them.
put
函数与say
函数几乎相同,但它在表达式上调用了.Str
函数.这会产生警告,因为您无法真正对类型对象进行字符串化.观察差异:
The put
function is almost the same as the say
function, but it calls the .Str
function on the expression. And that produces a warning, because you cannot really stringify a type object. Observe the difference:
$ raku -e 'say Any'
(Any)
# raku -e 'put Any'
Use of uninitialized value of type Any in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something meaningful.
in block <unit> at -e line 1
See Classes and Objects, Type System, Type Objects for more information.
这篇关于Raku 中 (Any) 的含义是什么 - 特别是 ()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!