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.)

- 413,195
- 112
- 811
- 826
3 Answers
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.

- 166,841
- 26
- 322
- 407
-
3`_` is used to separate groups of word characters from symbol characters. So `foo_!!_bar` is a legal identifier. If you don't want to follow this rule, or need to use a reserved word, you can also enclose it in backticks. This is useful for calling java methods named `type`, for example. – retronym Jun 22 '10 at 21:16
-
2@retronym - Good point about backticks; I'll add that. Try typing `val foo_!!_bar = 0` into the REPL, though.... – Rex Kerr Jun 22 '10 at 21:24
-
Oh wow, my mental parser is out of sync with the spec. Please ignore. Identifers are basically `\w[\w\d]*(_([:opchar:]*))` – retronym Jun 22 '10 at 21:46
-
Mhh, So, I understand that it is not possible to type for instance `def sort!( ... ` but you would have to use: `def sort_!(...` is that correct? – OscarRyz Jun 22 '10 at 21:53
-
@S-mSO - That's right. `def sort!()` is invalid. `def sort_!()` is fine. – Rex Kerr Jun 22 '10 at 23:06
-
@retronym: more like `\w[\w\d]*(_([:opchar:]*))?|[:opchar:]+` I think (including $ but this one is reserved). Also I just found out that // and /* cannot be operators... – PhiLho Sep 05 '10 at 07:19
There 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.

- 20,435
- 3
- 55
- 76
-
2Another similar convention is to use to use zero parameter lists for side-effect free functions, and one empty parameter list for others. e.g. `gun.model` vs `gun.fire()` – retronym Jun 22 '10 at 21:19
-
2
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

- 12,138
- 1
- 41
- 51
-
2It has more to do with unary_ then ! in the end. You can have unary_+ for example – manocha_ak Feb 13 '13 at 13:49