Billz
Billz

Reputation: 8125

In Scala how do you write a init function?

For example, in Ruby I can write

def initialize(price)
    @Current_price = price
end

Upvotes: 8

Views: 13276

Answers (1)

om-nom-nom
om-nom-nom

Reputation: 62835

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.

Upvotes: 16

Related Questions