0

Assuming there exists the following java class:

public class Test {
    static class Builder<B extends Builder<B>>{
        B asBuilder() {
            return (B) this;
        }
    }

    public static <B extends Builder<B>> B newBuilder() {
        return new Builder<B>().asBuilder();
    }
}

Trying to call Test.newBuilder() in a consuming Kotlin code gives the error Type expected.

Test.newBuilder<>() has the same issue. Test.newBuilder<Test.Builder>() gives the error: One type argument expected for class Builder<B : Test.Builder<B!>!>. Since the type argument is a recursive call this can't be solved in the above fashion.

I believe this is a rather weird behavior even from Java perspective. It's strange that the Test class code was even allowed in its current form. Unfortunately, the above was a simplified version of another class that I have no control of. In reality I am trying to do

org.apache.logging.log4j.core.layout.GelfLayout.newBuilder()
koikahin
  • 23
  • 4
  • 1
    You seem to be able to call this from Kotlin only if you know the *exact subclass of `Builder`* that it casts itself into. – EpicPandaForce Oct 19 '18 at 23:36
  • If I understand correctly, using it from Java seems to rely on raw types, which aren't available in Kotlin (https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Alexey Romanov Oct 20 '18 at 06:48
  • @EpicPandaForce right. I believe in the current instance the Builder is circumventing the type system to return a raw type. Kotlin doesn't seem to play well with it. – koikahin Oct 20 '18 at 22:48

0 Answers0