Do methods ending with _!
such as delete_!
or i_is_!
have a special meaning? Are they "just names"? Do they follow some convention? There's even bulkDelete_!!
. (The specific context is Lift if it makes a difference.)
views:
202answers:
3There are no special meanings to the ! in Scala names. In the family of Lisp-based languages, ! is often used to indicate that a function is has side-effects, and that looks to be the convention here.
I'm not sure what the convention is for using _!
and _!!
in Lift, but here's a bit of background.
Any alphanumeric identifier can have _ and a list of symbols added and still be parsed as a single identifier. For example:
scala> class Example_!@%*!
defined class Example_$bang$at$percent$times$bang
(In fact, you can parse almost anything as an identifier if you surround it with backticks--and this is what you do if a Java class uses a Scala reserved word, for example. Or if you want spaces in your identifiers.)
The compiler only recognizes one symbolic ending specially, however. If there is a method that looks like a getter, then getter_= will be interpreted as a setter. (Whether you actually use it as a setter is up to you; it will have the semantics of a setter, anyway.) So
scala> class Q { def q = "Hi"; def q_=(s: String) { println(s.reverse) } }
defined class Q
scala> val q = new Q
q: Q = Q@b5c12e
scala> q.q
res0: java.lang.String = Hi
scala> q.q = "Could use this to set something"
gnihtemos tes ot siht esu dluoC
In addition, the compiler reverses the order of caller and callee in any method that ends in :
. This is most often seen in lists: newElement :: existingList
is actually a call to existingList.::(newElement)
. So, for example:
scala> object Caps { def to_:(s: String) = s.toUpperCase }
defined module Caps
scala> "Example" to_: Caps
res40: java.lang.String = EXAMPLE
Any other usage of _
+ symbols is convention.
Strangely unmentioned thus far (though not particularly relevant to your question) is unary_! which is treated specially.
scala> class A { def unary_! = 5 }
defined class A
scala> !(new A)
res0: Int = 5