Using generic Kotlin fun from Java (and other platforms)

I am working on the KMP library, which should be called from multiple platforms.

Starting with multiplatform-library-template, I try to add Java code to call CommonMain Kotlin fun. The function declared as “val fibi = sequence { … }” creating the Sequence of Fibonacci numbers.

IDEA suggesting “Sequence<Integer> fibi = CustomFibiKt.getFibi();”, but fails to assign the value because method returns “Sequence<Int>” and Java complains:

Required Type: Sequence<java.lang.Integer>

Provided: Sequence

public val fibi: Sequence<Int>

I guess it doesn’t match java.lang.Integer to Kotlin’s Int

This may be an IDEA bug? It should work I think

Hello, would you have an example code to share?

“CustomFibi.kt” file in CommonMain/kotlin contains:

package com.ds.kmp.fibi

val fibi = sequence {
    var a = firstElement
    yield(a)
    var b = secondElement
    yield(b)
    while (true) {
        val c = a + b
        yield(c)
        a = b
        b = c
    }
}

expect val firstElement: Int
expect val secondElement: Int

“FibiJava.java” file in jvmMain/java contains:

package com.ds.kmp.fibi;

import kotlin.sequences.Sequence;

public class FibiJava {
    //actual val firstElement: Int = 0
//actual val secondElement: Int = 1
    private static Integer firstElement = 0;
    private static Integer secondElement = 1;

    private static Sequence<Integer> tmp;

    static void main(String[] args) {
//    firstElement = Int(args[1])
//    secondElement = Int(args[2])

        Sequence<Integer> fibi = CustomFibiKt.getFibi();
        fibi.iterator();
        System.out.println("tmp"); // .take(6).last());
    }
}

… and reports the error in the last assignment statement.