I’m working with a java library that does somethin...
# getting-started
b
I’m working with a java library that does something like this:
Copy code
class Foo {
    public static Foo.Builder Builder() { ... }
    public static class Builder { 
        public Builder() { ... }
    }
}
and i’m trying to call it from kotlin like:
Copy code
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
Sorry, I misread 🙂
👍 1
b
i did mutter some curses at the lib developers, as is tradition.
r
Always a recommended first step.
b
calling this code from java does work, which is surprising. i’d think it would be problematic there, too.
e
hmm. you can sorta disambiguate in one way,
Copy code
Foo.Builder::class
but that doesn't really help
b
but i’m in a pure kotlin project now and would hate to open the java gates for that
e
Java requires a
new
keyword to call the constructor while Kotlin treats them all as callables in the same namespace
b
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
if you don't mind importing kotlin.reflect and bypassing the function, you can
Copy code
Foo.Builder::class.createInstance()
doing the same for the function is probably doable, but it would have to look more like
Copy code
Foo::class.declaredFunctions.single { it.name == "Builder" }.call() as Foo.Builder
b
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