Reputation: 1878
I'm provided a class Foo
which does some work()
:
open class Foo() {
fun work(x: T) {
// Effects taking place here
}
}
And I am also provided (but by someone else) a method useJob()
which consumes an object of interface type Bar
having a single method doJob()
.
fun useJob(bar: Bar)
interface Bar {
fun doJob(x: T)
}
It turns out Foo.work()
does the job expected by useJob()
. However, in order to have useJob()
calling work()
, I need to write something like that:
useJob(object : Foo(), Bar { fun doJob(x: T) { work(x) } })
Is there any way to use a lambda instead of this blob?
EDIT: @jrtapsell comment made me realize Foo is actually open.
Upvotes: 0
Views: 210
Reputation: 7001
You could do it like this:
// Mock class
class Param
// Provided code
open class Foo<T> {
fun work(x: T) {
// Effects taking place here
}
}
fun useJob(bar: Bar) {}
interface Bar {
fun doJob(x: Param)
}
// New code
object FooBar: Foo<Param>(), Bar {
override fun doJob(x: Param) = work(x)
fun use() = useJob(this)
}
fun x() {
FooBar.use()
}
It requires a bit more code for the FooBar object, but it cleans up the call sites.
Upvotes: 0
Reputation: 170723
If Bar
were defined in Java, you could write
useJob { Foo().work(x) }
or
val foo = Foo()
useJob { foo.work(x) }
to avoid constructing Foo()
every time in case useJob
calls its argument multiple times.
But
Without moving Bar
to Java, I'd go with joecks' solution or define an overload of useJob
(possibly as extension method). Which is better depends on how many methods like useJob
you have and how many uses for each.
Upvotes: 1
Reputation: 4637
Well the easiest way to achieve this would be by using a factory method that creates a instance of Bar and accepts a function call:
fun job(func: (Param) -> Unit) : Bar = object: Bar {
override fun doJob(x: Param) = func(x)
}
then you can use
useJob( job { Foo().work(it) } )
Upvotes: 4
Reputation: 81879
It's a problem, that useJob
is expecting an interface instead of a function type directly. This way you can only do:
val bar = object : Bar {
override fun doJob(x: String) = Foo().work(x)
}
useJob(bar)
Upvotes: 1