在 Perl 6 中的方法和函数调用中使用冒号 [英] Use of colon in method and function calls in Perl 6

查看:47
本文介绍了在 Perl 6 中的方法和函数调用中使用冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道冒号与 Perl 6 中的方法和函数调用有什么关系.作为记录,我使用的是基于 MoarVM 版本 2015.05 构建的 perl6 版本 2015.05-55-gd84bbbc.

I'm wondering what colons have to do with method and function calls in Perl 6. For the record, I am using perl6 version 2015.05-55-gd84bbbc built on MoarVM version 2015.05.

我刚刚在 Perl6 规范测试(S32-io)(我添加了评论):

I just saw the following in a Perl6 spec test (S32-io) (I added the comment):

$fh.print: "0123456789A";   # prints '0123456789A' to the file

据我所知,这相当于:

$fh.print("0123456789A");   # prints '0123456789A' to the file

这两个似乎都需要多个参数并且可以很好地扁平化列表:

Both of these seem to take multiple arguments and to flatten lists fine:

$fh.print: "012", "345", "6789A";   # prints '0123456789A' to the file
$fh.print("012", "345", "6789A");   # prints '0123456789A' to the file

my @a = <012 345 6789A>;

$fh.print(@a);   # prints '0123456789A' to the file
$fh.print: @a;   # prints '0123456789A' to the file

使用这两种不同的语法肯定是有原因的.是否有任何理由使用一种或另一种语法?

There must be some reason to have these two different syntaxes. Is there any reason to use one or the other syntax?

我还注意到,当用作方法时,我们必须将 :() 与打印一起使用:

I also noticed that we have to use either : or () with print, when used as a method:

$fh.print(@a);   # Works
$fh.print: @a;   # Works!
$fh.print @a;    # ERROR!

在函数 print 中使用冒号时还有一些有趣的行为.在这种情况下,:() 是不等价的:

There is also some interesting behavior when using a colon with the function print. In this case, : and () are not equivalent:

print @a;  # Prints '0123456789A' (no newline, just like Perl 5)
print(@a); # Ditto
print: @a; # Prints '012 345 6789A' followed by a newline (at least in REPL)

print  @a, @a; # Error (Two terms in a row)
print: @a, @a; # Prints '012 345 6789A 012 345 6789A' followed by a newline (in REPL) 

然后我尝试在脚本文件中使用打印.这适用于打印到标准输出:

Then I tried using print in a script file. This works for prints to standard output:

print @a;

但是,这不会打印到标准输出:

However, this does not print to standard output:

print: @a, @a;

但方法版本工作正常:

$fh.print: @a, @a; # Prints '0123456789A0123456789A' to the file

我觉得我几乎明白这一点,但我无法用语言表达.有人可以解释一下这些使用印刷品的种类吗?此外,这些行为是否会因 Great List Refactor 而改变?

I feel like I almost understand this, but I can't put it into words. Could someone please explain these varieties of using print. Also, are these behaviors going to change due to the Great List Refactor?

推荐答案

使用冒号而不是括号的主要原因之一是它可以通过删除一组括号来帮助整理您的代码.否则它们完全相同.

One of the main reasons to use a colon instead of parens is that it can help declutter your code, by removing one set of parens. Otherwise they are exactly the same.

当你有 print: @a 时,你真正在做的是在行上放置一个标签,让 @a 落下.REPL 中的哪个将使用值调用 say.

When you have print: @a, what you are really doing is putting a label on the line, and letting the @a fall-through. Which in the REPL will call say with the value.

如果您在方法调用中不使用括号或冒号,则该方法将在没有参数的情况下被调用.

If you don't use parens or a colon on a method call, then the method would get called with no arguments.

如果使用冒号,您可以交换方法和调用者的顺序.

You can swap the order of the method, and the invocant if you use a colon.

say $*ERR: 'hello world'; # $*ERR.say('hello world')

这篇关于在 Perl 6 中的方法和函数调用中使用冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆