Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does () mean in Scala?

Tags:

scala

I've found a Scala code snippet which declares a method <init> and puts () right below the invocation.

I have a question about line number 5. What does () mean here?

(() => {
  final class $anon extends MutableProjection {
    def <init>() = {
      super.<init>();
      ()
    };
    ...
  };
  new $anon()
})

Here is a code with full example.

like image 353
Finkelson Avatar asked Sep 24 '15 22:09

Finkelson


People also ask

What is the meaning of => in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .

What does _ _1 mean in Scala?

_1 is a method name. Specifically tuples have a method named _1 , which returns the first element of the tuple.

Can you do += in Scala?

+= and similar operators are desugared by the compiler in case there is a + defined and no += is defined. (Similarly works for other operators too.) Check the Scala Language Specification (6.12. 4):

What is some function in Scala?

Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some. We can use this with the collection.


2 Answers

Every Scala function has a return type. By convention (and highly encouraged by some language features), functions that don't need to return anything have a return type called Unit, which has a singleton value written as ().

The last expression in a function body is its return value. That author made this be () to cause the compiler to infer that the return type should be Unit. But it would have been more clear to just do that with a type annotation. If a function's return type is Unit, Scala will implicitly return () from the function no matter what the last statement in the body is. So this

def <init>() = {
  super.<init>()
  ()
}

could be written equivalently as

def <init>(): Unit = super.<init>()
like image 71
Chris Martin Avatar answered Nov 09 '22 21:11

Chris Martin


() can meean a few things, depending on context.

As a value, it is an empty tuple, or the singleton type. It's type is Unit.

It can denote a function or method that takes no parameters, such as:

def foo() = "Hello world"

Note that when writing an anonymous function, the () is by itself but still means a function with no parameters.

val x = () => 2

The type of x is () => Int, a function taking no parameters and returning an int.

As a source of infinite confusion, you can get examples like this:

val y = () => ()

The type of y here is () => Unit, a function of no parameters returning Unit, not Unit => Unit, which would be writen as val z = (x:Unit) => (), and called like z(())

The unit vs empty parameter distinction has been awkward for me in the past so hopefully that demystifies some of it.

like image 34
triggerNZ Avatar answered Nov 09 '22 21:11

triggerNZ