Kotlin 中文文档 v2.0.10 Help

Testing page

Synchronized tabs

plugins { kotlin("kapt") version "1.9.23" }
plugins { id "org.jetbrains.kotlin.kapt" version "1.9.23" }
plugins { kotlin("plugin.noarg") version "1.9.23" }
plugins { id "org.jetbrains.kotlin.plugin.noarg" version "1.9.23" }

Sections

Collapsed section

Some text here and a codeblock:

plugins { kotlin("plugin.noarg") version "1.9.23" }

Codeblocks

Just a codeblock:

import java.util.* @Service class MessageService(val db: MessageRepository) { fun findMessages(): List<Message> = db.findAll().toList() fun findMessageById(id: String): List<Message> = db.findById(id).toList() fun save(message: Message) { db.save(message) } fun <T : Any> Optional<out T>.toList(): List<T> = if (isPresent) listOf(get()) else emptyList() }

Expandable codeblock

package com.example.demo import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController @SpringBootApplication class DemoApplication fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @RestController class MessageController { @GetMapping("/") fun index(@RequestParam("name") name: String) = "Hello, $name!" }

Runnable codeblock

data class User(val name: String, val id: Int) fun main() { val user = User("Alex", 1) //sampleStart // Automatically uses toString() function so that output is easy to read println(user) // User(name=Alex, id=1) //sampleEnd }

Tables

Markdown table

Primitive-type array

Equivalent in Java

BooleanArray

boolean[]

ByteArray

byte[]

CharArray

char[]

DoubleArray

double[]

FloatArray

float[]

IntArray

int[]

LongArray

long[]

ShortArray

short[]

XML table

Last modified on

December 2023

Next update

June 2024

XML table with codeblocks inside

Simple table:

Before

Now

kotlin { targets { configure(['windows', 'linux']) { } } }
kotlin { targets { configure([findByName('windows'), findByName('linux')]) { } } }

More complex table:

Before

Now

Dependencies of the jvmMain compilation

jvm<Scope>
jvmCompilation<Scope>
dependencies { add("jvmImplementation", "foo.bar.baz:1.2.3") }
dependencies { add("jvmCompilationImplementation", "foo.bar.baz:1.2.3") }

Dependencies of the jvmMain source set

jvmMain<Scope>

Dependencies of the jvmTest compilation

jvmTest<Scope>
jvmTestCompilation<Scope>

Dependencies of the jvmTest source set

jvmTest<Scope>

Lists

Ordered list

  1. One

  2. Two

  3. Three

    1. Three point 1

    2. Three point 2

    3. Three point 3

      1. Three point 1 and 1

  4. With a codeblock inside:

    jvmTest<Scope>

Non-ordered list

  • First bullet

  • Second bullet

  • Third bullet

    • One more

    • Another one

      • Wow, one more

  • With a codeblock inside:

    jvmTest<Scope>

Definition list

Collapsible item #1

The return type of the findById() function in the CrudRepository interface is an instance of the Optional class. However, it would be convenient to return a List with a single message for consistency. For that, you need to unwrap the Optional value if it’s present, and return a list with the value. This can be implemented as an extension function to the Optional type.

In the code, Optional<out T>.toList(), .toList() is the extension function for Optional. Extension functions allow you to write additional functions to any classes, which is especially useful when you want to extend functionality of some library class.

Collapsible item #2

This function works with an assumption that the new object doesn’t have an id in the database. Hence, the id should be null for insertion.

If the id isn’t null, CrudRepository assumes that the object already exists in the database and this is an update operation as opposed to an insert operation. After the insert operation, the id will be generated by the data store and assigned back to the Message instance. This is why the id property should be declared using the var keyword.

Text elements

Variables

  • variable using: latest Kotlin version is 2.0.10

Embedded elements

Video from YouTube

Pictures

Regular (Markdown):

Create a test

Regular (XML):

Multiplatform web wizard

Inline:

YouTrack

Zoomable:

class diagram

Button-style:

Create a project

Notes

Warning:

Note:

Tip:

Last modified: 08 九月 2024