Hi, I try to make a Builder in Kotlin, all was fin...
# announcements
j
Hi, I try to make a Builder in Kotlin, all was fine with this sample code I inspirate from, but my goal is to consume it in Java (Kotlin default parameters = Overload methods), so the Builder pattern in Kotlin is not usefull to use in Java with this sample code. what alternatives I have ?
j
Florent, my goal is to consume it in java. Tthe stakoverflow post don't talk about it
m
Look at this section of the docs anytime you're trying to do interop. Lots of cases covered. https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#calling-kotlin-from-java For you, I think @JvmOverloads may help. https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#overloads-generation
j
@JvmOverloads
don't help, because its generate Overload methods (like I said in my initial post), so Builder pattern bring nothing if I must pass all "options" in parameters of the builder.
m
Sorry, didn't read closely enough. Hmm. If the builder is only used in one Kotlin module, you could mark the constructor as
internal
. Then the constructor name should be 'hidden' to Java by name mangling, so the only options would be the builder syntax. But you could still use it in Kotlin as-is.
s
A friend has developed a Builder via annotation implementation: https://github.com/ThinkingLogic/kotlin-builder-annotation
f
That was on the stackoverflow answer, I guess he isn't interested by this solution 🤷‍♂️