https://kotlinlang.org logo
Title
k

Koneko Toujou

02/06/2023, 3:17 PM
what does
expect
and
actual
translate to in java eg
expect fun getPlatformName(): String
actual fun getPlatformName(): String {
    return "Desktop"
}
h

hfhbd

02/06/2023, 3:22 PM
https://kotlinlang.org/docs/multiplatform-connect-to-apis.html#rules-for-expected-and-actual-declarations expect/actual are language keywords and not visible in the resulting target code.
k

Koneko Toujou

02/06/2023, 3:23 PM
hmm ok could i do the same thing in Java ?
as i am unable to mix java and kotlin in the same module
l

Landry Norris

02/06/2023, 3:24 PM
Java doesn’t have different platform targets, so I don’t believe there is an equivalent. It’s all JVM bytecode
k

Koneko Toujou

02/06/2023, 3:24 PM
alright
h

hfhbd

02/06/2023, 3:25 PM
expect/actual are used for Kotlin Multiplatform only to enable consuming a common declaration in your common code.
k

Koneko Toujou

02/06/2023, 3:26 PM
🙂
l

Landry Norris

02/06/2023, 3:27 PM
Why can’t you mix java and Kotlin in the same module?
g

Goetz Markgraf

02/06/2023, 3:27 PM
The best analogy is
interface
and
class
but it is not a translation but only another implementation of a similar concept.
k

Koneko Toujou

02/06/2023, 3:28 PM
it doesnt seem to compile the java code for some reason (class def not found)
l

Landry Norris

02/06/2023, 3:28 PM
Have you set up the java plugin or added withJava to your jvm block in the kotlin block?
k

Koneko Toujou

02/06/2023, 3:30 PM
i dont think so
l

Landry Norris

02/06/2023, 3:31 PM
kotlin {
    jvm {
        withJava()
    }
}
Note you can’t have this and an android target in the same module.
k

Koneko Toujou

02/06/2023, 3:42 PM
so for example, i cannot put it in this?
plugins {
    kotlin("multiplatform")
    id("org.jetbrains.compose")
    id("com.android.library")
}

kotlin {
    android()
    jvm("desktop") {
        jvmToolchain(11)
    }
    sourceSets {
        val commonMain by getting { dependencies { ... } }
        val androidMain by getting { dependencies { ... } }
        val desktopMain by getting { dependencies { ... } }
    }
}

android {
    namespace = "smallville7123.lua.jit.kotlin.common"
    compileSdkVersion(33)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(33)
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}
l

Landry Norris

02/06/2023, 3:43 PM
You can try, but the android plugin and the java plugin are incompatible. Why do you need Java code? IntelliJ can convert it to Kotlin.
k

Koneko Toujou

02/06/2023, 3:45 PM
easier to write in java 🙂
imma just have both a java module and a kotlin module
l

Landry Norris

02/06/2023, 3:45 PM
You won’t be able to expect/actual like that. I’d create an interface, then implement it in each platform and pass in a factory or something.
k

Koneko Toujou

02/06/2023, 3:50 PM
true, but i should be able to expect/actual via seperate kotlin + java modules eg
kotlin-impl-bindings-desktop
java-impl-desktop

kotlin-impl-bindings-android
java-impl-android
however the
:common
module would require both
desktop
and
android
modules but i think it wont actually build both if only building for a single platform?
l

Landry Norris

02/06/2023, 3:54 PM
You could have your Kotlin module depend on your java module, then expect/actual creating it. Since you’re doing a Kotlin module anyway, I’d highly recommend learning to write in Kotlin directly instead of doing the Java modules.
k

Koneko Toujou

02/06/2023, 3:54 PM
alright