Benutzer-Werkzeuge

Webseiten-Werkzeuge


kotlin

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Nächste Überarbeitung
Vorhergehende Überarbeitung
kotlin [2023-12-11 16:15:18] – angelegt manfredkotlin [2025-07-24 19:50:19] (aktuell) david
Zeile 1: Zeile 1:
 +====== Kotlin ======
 +
 +===== Kotlin Scripting =====
 +
 +  * endung: ''.main.kts'' (siehe [[https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/main-kts/MainKts.md]])
 +  * ausführen:
 +    * ''./hello-world.main.kts'' (mit shebang: ''#!/usr/bin/env kotlin'')
 +    * ''kotlin hello-world.main.kts''
 +    * ''kotlinc -script hello-world.main.kts''
 +
 +==== Beispiele ====
 +
 +=== JSON Serialization ===
 +
 +die annotation ''%%@file:CompilerOptions%%'' unterstützt aktuell noch nicht ''-Xplugin'',
 +daher muss man das kotlin script mit ''kotlin -Xplugin'' starten und kann das shebang nicht verwenden
 +
 +  > # ./json.main.kts deserialize
 +  > kotlin_home="/opt/homebrew/Cellar/kotlin/2.2.0"
 +  > kotlin -Xplugin="${kotlin_home}/libexec/lib/kotlin-serialization-compiler-plugin.jar" json.main.kts -- deserialize
 +  Data(int=42, bool=true, string=cool, nullable=null)
 +
 +  > # ./json.main.kts serialize
 +  > kotlin_home="/opt/homebrew/Cellar/kotlin/2.2.0"
 +  > kotlin -Xplugin="${kotlin_home}/libexec/lib/kotlin-serialization-compiler-plugin.jar" json.main.kts -- serialize
 +  {
 +      "int": 42,
 +      "bool": true,
 +      "string": "cool",
 +      "nullable": null
 +  }
 +
 +<code kotlin json.main.kts>
 +#!/usr/bin/env kotlin
 +
 +// not yet supported
 +// @file:CompilerOptions("-Xplugin=kotlin-serialization-compiler-plugin.jar")
 +@file:Repository("https://repo.maven.apache.org/maven2")
 +@file:DependsOn(
 +    "org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0"
 +)
 +
 +import kotlinx.serialization.*
 +import kotlinx.serialization.json.*
 +
 +val json = Json {
 +    ignoreUnknownKeys = true
 +    prettyPrint = true
 +}
 +
 +val command = args
 +    .firstOrNull()
 +    ?.lowercase()
 +    ?.let { Commands.of(it) }
 +
 +when (command) {
 +    Commands.Deserialize -> deserialize()
 +    Commands.Serialize -> serialize()
 +    else -> {
 +        val name = (this::class.simpleName ?: "*")
 +            .lowercase()
 +            .replace("_main", "")
 +            .replace("_", "-")
 +            .let { "${it}.main.kts" }
 +        val cmds = Commands.entries
 +            .joinToString(separator = "|", prefix = "<", postfix = ">") {
 +                it.name.lowercase()
 +            }
 +        "Usage: $name $cmds"
 +    }
 +}.also { result ->
 +    println(result)
 +}
 +
 +fun deserialize(): String {
 +    val serialized = """
 +        {
 +            "int": 42,
 +            "bool": true,
 +            "string": "cool",
 +            "nullable": null
 +        }
 +    """.trimIndent()
 +    val deserialized = json.decodeFromString<Data>(serialized)
 +    return deserialized.toString()
 +}
 +
 +fun serialize(): String {
 +    val deserialized = Data(
 +        42,
 +        true,
 +        "cool",
 +        null,
 +    )
 +    val serialized = json.encodeToString(deserialized)
 +    return serialized
 +}
 +
 +enum class Commands {
 +    Deserialize,
 +    Serialize;
 +
 +    companion object {
 +        fun of(value: String?) = entries.find { it.name.lowercase() == value?.lowercase() }
 +    }
 +}
 +
 +@Serializable
 +data class Data(
 +    val int: Int,
 +    val bool: Boolean,
 +    val string: String,
 +    val nullable: String?,
 +)
 +</code>
 +
 +
 +===== Kotlin/JVM =====
 +
 +===== Kotlin/Native =====
 +
 +==== FreeBSD ====
 +
 +Installation:
 +  > cd /usr/ports/java/openjdk21 && make clean install clean
 +  
 +  > git clone git@github.com:JetBrains/kotlin.git ~/kotlin
 +  > cd ~/kotlin
 +  
 +  > vim local.properties
 +  kotlin.build.isObsoleteJdkOverrideEnabled=true
 +  kotlin.native.enabled=true
 +  org.gradle.java.installations.auto-download=false
 +  
 +  > ./gradlew :kotlin-native:bundle
 +
 +Test-Skript:
 +  > vim /tmp/hello.kt
 +  fun main() {
 +      println("Hello Kotlin/Native!")
 +  }
 +
 +Kompilertest:
 +  > ~/kotlin/kotlin-native/dist/bin/kotlinc-native" /tmp/hello.kt -o /tmp/hello
 +  > /tmp/hello.kexe
 +