https://kotlinlang.org logo
Title
b

bbaldino

12/10/2021, 10:18 PM
I’m working with a java library that does something like this:
class Foo {
    public static Foo.Builder Builder() { ... }
    public static class Builder { 
        public Builder() { ... }
    }
}
and i’m trying to call it from kotlin like:
Foo.Builder()
...
but i get complaints about ambiguity…i think because the helper type in
Foo
matches the class name (and therefore the constructor) exactly. does anyone know if there’s a way to disambiguate between the two from kotlin?
unfortunately out of my control 😕
r

Ruckus

12/10/2021, 10:20 PM
Sorry, I misread 🙂
👍 1
b

bbaldino

12/10/2021, 10:21 PM
i did mutter some curses at the lib developers, as is tradition.
r

Ruckus

12/10/2021, 10:21 PM
Always a recommended first step.
b

bbaldino

12/10/2021, 10:21 PM
calling this code from java does work, which is surprising. i’d think it would be problematic there, too.
e

ephemient

12/10/2021, 10:22 PM
hmm. you can sorta disambiguate in one way,
Foo.Builder::class
but that doesn't really help
b

bbaldino

12/10/2021, 10:22 PM
but i’m in a pure kotlin project now and would hate to open the java gates for that
e

ephemient

12/10/2021, 10:22 PM
Java requires a
new
keyword to call the constructor while Kotlin treats them all as callables in the same namespace
b

bbaldino

12/10/2021, 10:23 PM
Ah ha…good catch
hmm…i wonder if with ::class i could grab the constructor and call it like that. ugly, but workable maybe
e

ephemient

12/10/2021, 10:23 PM
if you don't mind importing kotlin.reflect and bypassing the function, you can
Foo.Builder::class.createInstance()
doing the same for the function is probably doable, but it would have to look more like
Foo::class.declaredFunctions.single { it.name == "Builder" }.call() as Foo.Builder
b

bbaldino

12/10/2021, 10:24 PM
ah yeah, i’ll probably just do that for now
createInstance
is probably workable
Ugh…so actually the real
Foo
class has generics, so I can’t use
::class
😕
I thought a typealias might work, but it looks like the static functions of the aliased type don’t resolve on the typealias?
maybe i’ll just have to write a subclass or wrapper class
ah, i take that back…the typealias did work