In Scala, how do you write the init function?

For example, in Ruby I can write

def initialize(price)
    @Current_price = price
end
+5
source share
1 answer

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

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

or simply

class Foo(val currentPrice: Int) {

}

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

+12
source

All Articles