在函数名称中使用特殊字符 [英] Use of special characters in function names

查看:136
本文介绍了在函数名称中使用特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中,标准约定是在方法名称的末尾使用问号,以指示该方法返回布尔结果:

In Ruby, a standard convention is to use a question mark at the end of a method name to indicate the method returns a boolean result:

[].empty?   #=> true 

另一种标准约定是:如果方法具有破坏性(即,它修改了原始数据),则该方法名称以感叹号结尾:

Another standard convention is to end a method name with an exclamation point if the method is destructive (that is, it modifies the original data):

mylist.sort! # sort mylist in-place

最近,我看到了Scheme中使用的这些相同约定.这让我想知道,还有哪些其他语言使用/支持该约定?这些语言或其他语言是否还有其他通常用于命名的特殊字符?

Recently I have seen these same conventions used in Scheme. Which makes me wonder, what other languages use/support this convention? Are there any other special characters that are commonly used for naming by these or other languages?

推荐答案

答案当然是特定于语言(和语言文化)的.

The answer is, of course, language (and language culture) specific.

例如,根据语言,以下所有内容均适用:empty-p,empty?,empty,is_empty或isEmpty. (这些示例当然不包括在内).

For example, depending on the language, all of the following are appropriate: empty-p, empty?, empty, is_empty or isEmpty. (These examples are, of course, not inclusive).

原始问题中的示例来自Ruby,在哪里这样使用?和 !结束时,方法名称应被接受.这种接受来自于1)在语法中作为符号终止符很容易获得的2)使用?.和 !在标准库中.但是,要注意的是!并非普遍用于暗示副作用",通常仅以替代形式出现:save/save!,sort/sort!等.存在大量执行副作用的方法.效果里面没有!前缀.

The examples in the original question come from Ruby where such use of ? and ! to end method names are, where appropriate, accepted. This acceptance comes from 1) readily accessible as symbol terminators in the grammar 2) use of ? and ! in the standard library. However, it should be note that ! is not used universally to imply "side-effect" and is generally only present on alternative forms: save/save!, sort/sort!, etc. There are an overwhelming number of methods that perform side-effects which do not have the ! prefix.

就我个人而言,如果我正在设计一种语言,我会允许?,!和'是符号名称中有效的不带引号的尾随字符.即使某些语言允许使用完整的符号转义符(例如Scala),也通常避免使用此类符号,因为不得不引用它们来使用会很痛苦.

Personally, if I was designing a language, I would allow ?, ! and ' to be valid unquoted trailing characters in symbol names. Even though that some languages allow full symbol escaping, such as Scala, such symbols are usually avoided because it's a pain to have to quote them for use.

// in Scala, esp. with Java-compat, the last form is generally used although
// the first two are perfectly valid -- do what makes sense in the language
foo.`empty?`
foo.empty_?
foo.isEmpty

在罗马时...

  • Scala-empty?还是empty_? (不常见)
  • C/C ++/JS/Java/Python/Perl-不?或者 !在标识符中; JS/Java允许$; $是Perl的标记
  • C/C ++/Perl/JS-举手吗?
  • C#-IsEmpty(方法或属性)或Empty(属性)
  • Python-每个Guido指南的is_empty或isEmpty(尽管通常为 len 协议:if not len(foo): "empty!")
  • Java-每个语言​​的isEmpty指南
  • Ruby-特殊结尾吗?和 ! (引用?)
  • JS-间接标识符访问:obj ["empty?"](对此不常见)
  • Lisp(常规):谓词为empty-p;读者/报价中的变化
  • Julia-追加!修改参数的函数
  • Scala - empty? or empty_? (not common)
  • C/C++/JS/Java/Python/Perl - no ? or ! in identifiers; JS/Java allow $; $ is a sigil in Perl
  • C/C++/Perl/JS - hands up in the air?
  • C# - IsEmpty (method or property) or Empty (property)
  • Python - is_empty or isEmpty per Guido-guide (although usually len protocol: if not len(foo): "empty!")
  • Java - isEmpty per Language Guide
  • Ruby - special trailing ? and ! (quoting?)
  • JS - indirect identifier access: obj["empty?"] (not common for this)
  • Lisp (conventional): empty-p for predicate; variations in reader/quoting
  • Julia - appends ! to functions that modify their arguments

很高兴看到更正和/或增加的内容–简直是九牛一毛.

Glad to see corrections and/or additions -- is only a drop in a bucket.

这篇关于在函数名称中使用特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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