I have some code that looks like this:
object Money {
def apply(dollars: BigInt, cents: BigInt) = new Money(dollars, cents)
}
class Money(dollars: BigInt, cents: BigInt) {
override def toString() = dollars + "." + cents
}
I'm trying to understand the benefits of the apply function but so far can only think of it as a type of syntactic sugar so that I don't need to use the new operator to construct a Money object. That is, I don't see a lot of benefit in the apply function, as all it's doing for me is turning this:
val money = new Money(2, 50)
into:
val money = Money(2, 50)
Am I missing something?