I have the following class representing some amount of money:
class Money(initDollars: Int, initCents: Int){
require (initDollars >= 0 && initCents >= 0)
private def this(positive: Boolean, initDollars: Int, initCents: Int) = {
this(initDollars, initCents)
//this.positive = positive
}
val positive: Boolean = true
val dollars = initDollars + initCents/100
val cents = initCents % 100
private val totalAmount = dollars * 100 + cents
def unary_- = new Money(!positive, dollars, cents)
}
object Money{
def apply(initDollars: Int, initCents: Int) = new Money(initDollars, initCents)
}
The amount can also be negative and I want to create it like this:
val am = -Money(1, 20)
So I want to initialize val positive
from a secondary constructor, but I can't do it, because it's reassignment to val. I also can't add val
in the list of parameters to secondary constructors. Could someone help?
Do it the other way around.
class Money private (pos: Boolean, initDollars: Int, initCents: Int) {
require (initDollars >= 0 && initCents >= 0)
def this(initDollars: Int, initCents: Int) = {
this(true, initDollars, initCents)
}
val positive: Boolean = pos
val dollars = initDollars + initCents/100
val cents = initCents % 100
private val totalAmount = dollars * 100 + cents
def unary_- = new Money(!positive, dollars, cents)
}
I think in order to achieve what you want, you have to do the following structural modification:
class Money(initDollars: Int, initCents: Int, positive: Boolean)
That way, you can then write a constructor that has less parameters without a problem (e.g. omitting "positive"). Doing it the other way around will be hard in Scala, if you want to stick to immutable values.
If you have the following declaration in your class body:
val positive: Boolean = true
You won't be able to change positive
anymore, no way around it :)
Switch to one constructor, add positive to the end and give it a default value of true like this:
positive:Boolean = true
This way if defaults to true if not supplied. Then do the same for your apply function and invoke the 3 field constructor in apply.
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