这两个函数调用约定有什么区别? [英] What's the difference these two function calling conventions?

查看:70
本文介绍了这两个函数调用约定有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过几种方式调用函数:

Functions can be called in a couple ways:

say(1, 2, 3) # 123
say: 1, 2, 3 # (1, 2, 3)

后者似乎通过了Positional,但是除此之外,我不知道它们之间还有什么不同.有什么重要的要知道的区别吗?您会在哪种情况下使用另一种情况?

The latter seems to pass a Positional, but apart from that I don't know how else they differ. Are there any differences that are important to know? What types of situations would you use one over the other?

推荐答案

正如Raiph在上文中告诉您的那样,say:是标签.因此,您没有say的任何内容(即使您认为自己做到了),并且-在REPL的外部使用-编译器会抱怨您对<a b c>的使用是无用的:

As Raiph tells you above, say: is a label. So you didn't say anything (even though you thought you did) and -- outside use of the REPL -- the compiler will complain that your use of <a b c> was useless:

say: <a b c>; # OUTPUT: «WARNINGS for <tmp>:␤Useless use of constant value a b c in sink context (lines 1, 1, 1, 1, 1, 1)␤»

但是,您经常可以在方法调用中使用:表示法而不是括号.考虑下面的四个例程调用(两个子例程调用,然后两个方法调用):

However, you often can use a : notation instead of parentheses in method calls. Consider the four routine calls below (two subroutine calls then two method calls):

my @numbers = (33, 77, 49, 11, 34);
say map  *.is-prime, @numbers  ;  # simplest subroutine call syntax
say map( *.is-prime, @numbers );  # same meaning, but delimiting args
say @numbers.map( *.is-prime ) ;  # similar, but using .map *method*
say @numbers.map: *.is-prime   ;  # same, but using : instead of parens

这些句子都将返回相同的(False False False True False).

These sentences will all return the same (False False False True False).

通常,正如您在上面使用map看到的那样,可以在使用:的任何地方在方法调用中使用(),但事实并非如此. :只能在方法调用中使用.

In general, as you see above with map, you can use () in method calls wherever you would use :, but the opposite is not true; : can be used only in method calls.

如果需要精确地定界参数,请使用(),如下文Raiph所述.

Use () if the arguments need to be delimited precisely, as Raiph comments below.

此答案侧重于基础知识.请参阅Raiph的答案,以更详尽地介绍例程调用语法的确切细节. (作为一个重要示例,如果例程名称和冒号(:)或左括号(()之间存在空格,则这些调用的含义通常会发生变化.)

This answer focuses on the basics. See Raiph's answer for more exhaustive coverage of the precise details of routine call syntax. (As an important example, the meaning of these calls normally changes if there's any spaces between the routine name and the colon (:) or opening parenthesis (()).

这篇关于这两个函数调用约定有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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