Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do methods ending with _! have a special meaning in Scala?

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

like image 931
aioobe Avatar asked Jun 22 '10 19:06

aioobe


People also ask

How do you define a method in Scala?

We can define a method's list of parameters in parentheses after its name. After the parameters list, we can provide a return type for the method with the leading colon sign. We then define a method's body after the equals sign. The compiler allows us to skip the curly braces if there is only one statement of code.

What is difference between function and method in Scala?

Difference between Scala Functions & Methods: Function is a object which can be stored in a variable. But a method always belongs to a class which has a name, signature bytecode etc. Basically, you can say a method is a function which is a member of some object.

What term would you use to define a function in Scala?

A Scala method is a part of a class which has a name, a signature, optionally some annotations, and some bytecode where as a function in Scala is a complete object which can be assigned to a variable. In other words, a function, which is defined as a member of some object, is called a method.


3 Answers

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.

like image 113
Dave Griffith Avatar answered Sep 21 '22 06:09

Dave Griffith


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.

like image 33
Rex Kerr Avatar answered Sep 20 '22 06:09

Rex Kerr


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
like image 44
psp Avatar answered Sep 19 '22 06:09

psp