Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala method and values names

Tags:

scala

Why this fails to compile:

scala> val a? = true
<console>:1: error: illegal start of simple pattern
   val a? = true
          ^

and this works?

scala>  val a_? = true
a_?: Boolean = true
like image 982
Rogach Avatar asked Dec 25 '11 11:12

Rogach


People also ask

What are methods 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.

What is call by value and call by-name in Scala?

In Scala when arguments pass through call-by-value function it compute the passed-in expression's or arguments value once before calling the function . But a call-by-Name function in Scala calls the expression and recompute the passed-in expression's value every time it get accessed inside the function.

What are by-name parameters in Scala?

By-name parameters are evaluated every time they are used. They won't be evaluated at all if they are unused. This is similar to replacing the by-name parameters with the passed expressions. They are in contrast to by-value parameters. To make a parameter called by-name, simply prepend => to its type.

Is Scala call by reference or value?

Java and Scala both use call by value exclusively, except that the value is either a primitive or a pointer to an object. If your object contains mutable fields, then there is very little substantive difference between this and call by reference.


1 Answers

According to the Scala language specification (looking at 2.8, doubt things have changed much since):

idrest ::= {letter | digit} [`_' op]

That is, an identifier can start with a letter or a digit followed by an underscore character, and further operator characters. That makes identifiers such as foo_!@! valid identifiers. Also, note that identifiers may also contain a string of operator characters alone. Consider the following REPL session:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_16).

scala> val +aff = true
<console>:1: error: illegal start of simple pattern
val +aff = true
^

scala> val ??? = true
???: Boolean = true

scala> val foo_!@! = true
foo_!@!: Boolean = true

scala> val %^@%@ = true
%^@%@: Boolean = true

scala> val ^&*!%@ = 42
^&*!%@: Int = 42

Hope this answers your question.

like image 175
S.R.I Avatar answered Sep 21 '22 03:09

S.R.I