For example, in Ruby I can write
def initialize(price)
@Current_price = price
end
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With