Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala underscores in names

Tags:

scala

I've read multiple style guidelines and other resources stating it's a bad idea to use underscores in method/variable/other names.

What are the technical reasons behind this?

I'm very accustomed to e.g. prepending helper functions with _. There are also functions that should be private that I want to make public so I can access them via the REPL. Other naming conventions like using a "helper" suffix just seem cumbersome.

Any thoughts would be appreciated!

like image 618
Pandora Lee Avatar asked Sep 01 '11 23:09

Pandora Lee


People also ask

Can you use underscores in variable names?

Variable names should not start with underscore ( _ ) or dollar sign ( $ ) characters, even though both are allowed. This is in contrast to other coding conventions that state that underscores should be used to prefix all instance variables.

What does _ means in Scala?

Scala allows the use of underscores (denoted as '_') to be used as placeholders for one or more parameters. we can consider the underscore to something that needs to be filled in with a value. However, each parameter must appear only one time within the function literal.

What does case _ mean in Scala?

case _ => does not check for the type, so it would match anything (similar to default in Java). case _ : ByteType matches only an instance of ByteType . It is the same like case x : ByteType , just without binding the casted matched object to a name x .

What does import ._ mean in Scala?

Module Import. We use underscore when importing packages to indicate that all or some members of the module should be imported: // imports all the members of the package junit. ( equivalent to wildcard import in java using *) import org.junit._ // imports all the members of junit except Before.


2 Answers

i think those specs just talk about underscores in names.

in what (sane) way should you write the following, if not this way?

object getterSetter {
    var _variable: Option[Double] = None
    def variable = _variable
    def variable_=(newVal: Double) { _variable = Some(newVal) }
}

i think the reason for camelCase instead of embedded_underscores is simply that java uses that and when working with java libraries, consistency can be only kept like that.

like image 109
flying sheep Avatar answered Oct 08 '22 13:10

flying sheep


The wildcard operator _ is used heavily in Scala. So:

xs map (_.x)  // Call the x method of every element
xs map (_x)   // Pass every element through the _x method

is confusing. You have to look very carefully to see if the underscore is prepended.

However, internal underscores are harder to confuse:

xs map (my_method)  // No similar form where _ stands for the list element

So these are less problematic, but the underscores still catch the eye a little when one is looking for closures. This is probably why they're discouraged, but to be honest, I use them all the time, particularly in things like implicit defs and internal variables where they won't have much or any exposure in an interface.

like image 23
Rex Kerr Avatar answered Oct 08 '22 14:10

Rex Kerr