The following Java 8 code passes a lambda to a function that defers execution of the generateMessage(...) function only if logging is enabled.
What would the equivalent Scala code look like?
producer.send(true, () -> generateMessage(1, "A Test Message"));
public void send(boolean enabled, Supplier<ProducerRecord> message) {
if (enabled) {
something.send(message.get())
}
}
This is compilable and runnable code. Hope it helps.
object HelloWorld {
def main(args: Array[String]) = {
send(true, () => "Foo")
def send(enabled: Boolean, supplier: () => String) =
if (enabled) somethingSend(supplier())
def somethingSend(message: String) = println(message)
}
}
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