what does `expect` and `actual` translate to in j...
# getting-started
k
what does
expect
and
actual
translate to in java eg
Copy code
expect fun getPlatformName(): String
Copy code
actual fun getPlatformName(): String {
    return "Desktop"
}
h
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
hmm ok could i do the same thing in Java ?
as i am unable to mix java and kotlin in the same module
l
Java doesn’t have different platform targets, so I don’t believe there is an equivalent. It’s all JVM bytecode
k
alright
h
expect/actual are used for Kotlin Multiplatform only to enable consuming a common declaration in your common code.
k
🙂
l
Why can’t you mix java and Kotlin in the same module?
g
The best analogy is
interface
and
class
but it is not a translation but only another implementation of a similar concept.
k
it doesnt seem to compile the java code for some reason (class def not found)
l
Have you set up the java plugin or added withJava to your jvm block in the kotlin block?
k
i dont think so
l
Copy code
kotlin {
    jvm {
        withJava()
    }
}
Note you can’t have this and an android target in the same module.
k
so for example, i cannot put it in this?
Copy code
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
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
easier to write in java 🙂
imma just have both a java module and a kotlin module
l
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
true, but i should be able to expect/actual via seperate kotlin + java modules eg
Copy code
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
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
alright