Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala how do you write a init function?

For example, in Ruby I can write

def initialize(price)
    @Current_price = price
end
like image 417
Billz Avatar asked Jul 24 '12 22:07

Billz


People also ask

What is INIT in Scala?

In Scala Stack class , the init() method is utilized to return a new stack that consists of all the elements except the last one. Method Definition: def init: Stack[A] Return Type: It returns a new stack that consists of all the elements except the last one.

What is function init ()?

The INIT function initializes the data structures required by the rest of the computation of the aggregate. For example, if you write a C function, the INIT function can set up large objects or temporary files for storing intermediate results.

Can you call __ init __?

You cannot call them explicitly. For instance, when you create a new object, Python automatically calls the __new__ method, which in turn calls the __init__ method.


1 Answers

In scala you write all init in class/trait/object body:

class Foo(price: Int) {
  val currentPrice = price
}

or simply

class Foo(val currentPrice: Int) {

}

You can think of class body as of primary constructor method, as DNA said.

like image 51
om-nom-nom Avatar answered Oct 15 '22 12:10

om-nom-nom