A Function0
requiring an explicit call:
producer.send(true, () => generateMessage(1, "A Test Message"));
def send(enabled: Boolean, message: () => ProducerRecord): Unit = {
if (enabled) {
something.send(message())
}
}
Or a parameter by name:
producer.send(true, generateMessage(1, "A Test Message"));
def send(enabled: Boolean, message: => ProducerRecord): Unit = {
if (enabled) {
something.send(message)
}
}
In the latter case, the message is evaluated every time it is used, if it is ever, but on the call site it looks like a regular method call, without having to manually transfer it to a function.
source
share