https://kotlinlang.org logo
Title
a

arekolek

01/24/2020, 8:30 AM
// 1
interface FooFactory {
    fun getFoo(): Foo
}

// 2
interface FooFactory : () -> Foo

// 3
interface FooFactory : () -> Foo {
    override fun invoke(): Foo
}
Is 2 or 3 something that people do? (not using a
typealias
here, because it wouldn't work with Dagger)
3️⃣ 1
2️⃣ 1
1️⃣ 4
m

Matteo Mirk

01/24/2020, 9:09 AM
I think 1️⃣ would be the preferred from a domain-modeling standpoint, while the other two only express a generic function intent. In this particular case maybe they won’t be bad since the interface name already conveys the meaning and a function name will probably be redundant. This said and considering Dagger constraint, maybe I’d prefer 2️⃣ for conciseness.
a

arekolek

01/24/2020, 10:07 AM
One issue with 2️⃣ that I see is that you can't look for invocations using "find usages" in IDE
m

Michael Friend

01/24/2020, 6:25 PM
You can also look out for Kotlin 1.4 release where they plan to introduce the functional interface which would probably be the prefered solution for your situation once it releases. https://blog.jetbrains.com/kotlin/2019/12/what-to-expect-in-kotlin-1-4-and-beyond/
👍 1