How do I run the refint1 function? I've triedvar x = new RefInt(5) and then doing scala> argpass.refint1(x)but get a found: RefInt, required : argpass.RefInt => Unit error in the console.
object argpass{
class RefInt (initial : Int) {
private var n : Int = initial
def get () : Int = n
def set (m : Int) : Unit = { n = m}
}
def refint1 ( f: RefInt => Unit) : (Int, Int, Int) = {
var x = new RefInt(5)
val first = f(x)
val firstget = x.get
val sec = f(x)
val secget = x.get
val third = f(x)
val thirdget = x.get
(firstget, secget, thirdget)
}
//How do i run the refint1 function?
fis a function fromRefInttoUnit(equivalent of Java'svoid). Thus, you need to pass a function, not a value. - Second, since the function returns anUnit, storing its result _(infirst,second&third) is meaningless. - Third,x, even if muted internally byf, is not reassigned, thus it may (should IMHO) be a val. - Finally you can callrefint1like this:refint1 { ri => ri.set(i); i += 1 }wherevar i = 0.