kotlin
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| kotlin [2023-12-11 16:15:18] – angelegt manfred | kotlin [2025-07-24 19:50:19] (aktuell) – david | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | ====== Kotlin ====== | ||
| + | |||
| + | ===== Kotlin Scripting ===== | ||
| + | |||
| + | * endung: '' | ||
| + | * ausführen: | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | ==== Beispiele ==== | ||
| + | |||
| + | === JSON Serialization === | ||
| + | |||
| + | die annotation '' | ||
| + | daher muss man das kotlin script mit '' | ||
| + | |||
| + | > # ./ | ||
| + | > kotlin_home="/ | ||
| + | > kotlin -Xplugin=" | ||
| + | Data(int=42, | ||
| + | |||
| + | > # ./ | ||
| + | > kotlin_home="/ | ||
| + | > kotlin -Xplugin=" | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | <code kotlin json.main.kts> | ||
| + | # | ||
| + | |||
| + | // not yet supported | ||
| + | // @file: | ||
| + | @file: | ||
| + | @file: | ||
| + | " | ||
| + | ) | ||
| + | |||
| + | import kotlinx.serialization.* | ||
| + | import kotlinx.serialization.json.* | ||
| + | |||
| + | val json = Json { | ||
| + | ignoreUnknownKeys = true | ||
| + | prettyPrint = true | ||
| + | } | ||
| + | |||
| + | val command = args | ||
| + | .firstOrNull() | ||
| + | ? | ||
| + | ?.let { Commands.of(it) } | ||
| + | |||
| + | when (command) { | ||
| + | Commands.Deserialize -> deserialize() | ||
| + | Commands.Serialize -> serialize() | ||
| + | else -> { | ||
| + | val name = (this:: | ||
| + | .lowercase() | ||
| + | .replace(" | ||
| + | .replace(" | ||
| + | .let { " | ||
| + | val cmds = Commands.entries | ||
| + | .joinToString(separator = " | ||
| + | it.name.lowercase() | ||
| + | } | ||
| + | " | ||
| + | } | ||
| + | }.also { result -> | ||
| + | println(result) | ||
| + | } | ||
| + | |||
| + | fun deserialize(): | ||
| + | val serialized = """ | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | """ | ||
| + | val deserialized = json.decodeFromString< | ||
| + | return deserialized.toString() | ||
| + | } | ||
| + | |||
| + | fun serialize(): | ||
| + | val deserialized = Data( | ||
| + | 42, | ||
| + | true, | ||
| + | " | ||
| + | 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? | ||
| + | } | ||
| + | } | ||
| + | |||
| + | @Serializable | ||
| + | data class Data( | ||
| + | val int: Int, | ||
| + | val bool: Boolean, | ||
| + | val string: String, | ||
| + | val nullable: String?, | ||
| + | ) | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Kotlin/JVM ===== | ||
| + | |||
| + | ===== Kotlin/ | ||
| + | |||
| + | ==== FreeBSD ==== | ||
| + | |||
| + | Installation: | ||
| + | > cd / | ||
| + | | ||
| + | > git clone git@github.com: | ||
| + | > cd ~/kotlin | ||
| + | | ||
| + | > vim local.properties | ||
| + | kotlin.build.isObsoleteJdkOverrideEnabled=true | ||
| + | kotlin.native.enabled=true | ||
| + | org.gradle.java.installations.auto-download=false | ||
| + | | ||
| + | > ./gradlew : | ||
| + | |||
| + | Test-Skript: | ||
| + | > vim / | ||
| + | fun main() { | ||
| + | println(" | ||
| + | } | ||
| + | |||
| + | Kompilertest: | ||
| + | > ~/ | ||
| + | > / | ||
| + | |||
