oleber
oleber

Reputation: 1079

Function as tuple parameter on 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?

Upvotes: 3

Views: 86

Answers (2)

Sean
Sean

Reputation: 519

Although the question has been answered, this blog post may inspire a few more ideas about creating a Scala "Noop": https://jazzy.id.au/2015/04/01/noop-monad.html

Upvotes: 0

Joan
Joan

Reputation: 4300

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

Upvotes: 3

Related Questions