Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function as tuple parameter on Scala

Tags:

scala

I tried to signify the problem and I make it worst. Lets give the complex code.

I'm calling a function like:

mySchedule(config, actorSystem.scheduler.schedule) {
    ...
}

the function is defined like:

def mySchedule(config: MyConfig, cb: (FiniteDuration, FiniteDuration) => (=> Unit) => Cancellable)(f : => Unit) = {
    val initialDelay = ...
    val interval = ...
    cb(initialDelay, interval)(f)
}

For doing the test I was willing to do something like

def noop: Unit = {}
val promiseSchedule = Promise[(FiniteDuration, FiniteDuration, => Unit)]()
mySchedule(
   config,
   {... promiseSchedule.success((initialDelay, interval, f))}
)(noop)

promiseSchedule.future.value must be_==(...)

How do I make this work?

like image 918
oleber Avatar asked Mar 05 '26 02:03

oleber


1 Answers

Try that:

def noop(): Unit = {}
val promiseSchedule = Promise[(Int, () => Unit)]()
// ...
promiseSchedule.success((1, noop))

The reason is because in a Tuple (just like in a case class) all members of the constructior are vals.
And it is not possible to store a by-name call in a val but only its value or a function.

Cheers

like image 84
Joan Avatar answered Mar 06 '26 17:03

Joan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!