Reputation: 45
I have a scala project with a Java dependency. I want to execute some code when a call is made to a java method that returns Void (specifically, I want to complete a future when this method is called). Is there any way to do this? The following compiles but throws an exception at runtime:
val startLF = SettableFuture.create[Unit]()
when(mockConsumer.start).thenAnswer(new Answer[Void]() {
override def answer(invocationOnMock: InvocationOnMock): Void = {
startLF.set()
Void.TYPE.newInstance()
}
})
Thanks!
Upvotes: 0
Views: 1339
Reputation: 30756
You could add an implicit Unit -> Void
conversion:
import scala.language.implicitConversions
implicit def unitToVoid(u: Unit): Void = null
Then anything that returns Unit
can be used in a place that expects Void
, e.g.:
def foo: Void = println("")
Upvotes: -1
Reputation: 16324
It seems that you can ascribe null
as Void
, e.g.
def foo: Void = null
So in your example, you can replace Void.TYPE.newInstance()
with null
or null: Void
to be clearer.
Upvotes: 2