人们为什么使用_?作为标识符后缀? [英] Why do people use _? as an identifier suffix?
问题描述
我开始阅读Lift框架源代码,发现使用methodName_?
之类的名称定义了很多方法,是否有约定_?
具有某些特殊含义?
I start reading Lift framework source code, I find that there're so many methods are defined using a name like methodName_?
, is there a convention that _?
has some special meaning?
def empty_? : Boolean = {}
推荐答案
?
表示这是一个谓词,一个返回Boolean
的函数.此约定可以追溯到Lisp,其中?
(方案),p
或-p
(其他Lisps,用相似"字母模拟问号)也表示谓词.认为它是一个问题,对象是空的吗?"
The ?
denotes that this is a predicate, a function returning Boolean
. This convention goes back to Lisp, where ?
(Scheme), p
or -p
(other Lisps, simulating the question mark with a "similar" letter) also denote predicates. Think of it as asking a question, "is the object empty?"
Scala仅允许使用_
分隔混合的标识符名称(包含字母数字和标点符号).例如,
Scala will only allow mixed identifier names (containing alphanumerics and punctuation) if you separate them by _
. E.g.,
scala> def iszero?(x : Int) = x == 0
<console>:1: error: '=' expected but identifier found.
def iszero?(x : Int) = x == 0
^
不起作用,但是
scala> def iszero_?(x : Int) = x == 0
iszero_$qmark: (x: Int)Boolean
确实.
这篇关于人们为什么使用_?作为标识符后缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!