diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.31.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.31.kt new file mode 100644 index 000000000..57d16cb4c --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.31.kt @@ -0,0 +1,20 @@ +//sampleStart +fun getStringLength(obj: Any): Int? { + if (obj is String) { + // `obj` is automatically cast to `String` in this branch + return obj.length + } + + // `obj` is still of type `Any` outside of the type-checked branch + return null +} +//sampleEnd + +fun main() { + fun printLength(obj: Any) { + println("Getting the length of '$obj'. Result: ${getStringLength(obj) ?: "Error: The object is not a string"} ") + } + printLength("Incomprehensibilities") + printLength(1000) + printLength(listOf(Any())) +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.33.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.33.kt new file mode 100644 index 000000000..be0298249 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.33.kt @@ -0,0 +1,19 @@ +//sampleStart +fun getStringLength(obj: Any): Int? { + // `obj` is automatically cast to `String` on the right-hand side of `&&` + if (obj is String && obj.length >= 0) { + return obj.length + } + + return null +} +//sampleEnd + +fun main() { + fun printLength(obj: Any) { + println("Getting the length of '$obj'. Result: ${getStringLength(obj) ?: "Error: The object is not a string"} ") + } + printLength("Incomprehensibilities") + printLength("") + printLength(1000) +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.1.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.1.kt deleted file mode 100644 index 144af42b1..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.1.kt +++ /dev/null @@ -1,14 +0,0 @@ -fun main() { -//sampleStart - val a: Int = 100 - val boxedA: Int? = a - val anotherBoxedA: Int? = a - - val b: Int = 10000 - val boxedB: Int? = b - val anotherBoxedB: Int? = b - - println(boxedA === anotherBoxedA) // true - println(boxedB === anotherBoxedB) // false -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.10.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.10.kt deleted file mode 100644 index dfa601050..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.10.kt +++ /dev/null @@ -1,8 +0,0 @@ -fun main() { -val str = "abcd" -//sampleStart -for (c in str) { - println(c) -} -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.11.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.11.kt deleted file mode 100644 index fba1c3f67..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.11.kt +++ /dev/null @@ -1,7 +0,0 @@ -fun main() { -//sampleStart - val str = "abcd" - println(str.uppercase()) // Create and print a new String object - println(str) // the original string remains the same -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.12.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.12.kt deleted file mode 100644 index 0c69b6674..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.12.kt +++ /dev/null @@ -1,6 +0,0 @@ -fun main() { -//sampleStart -val s = "abc" + 1 -println(s + "def") -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.13.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.13.kt deleted file mode 100644 index 13b6321a8..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.13.kt +++ /dev/null @@ -1,6 +0,0 @@ -fun main() { -//sampleStart - val i = 10 - println("i = $i") // prints "i = 10" -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.14.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.14.kt deleted file mode 100644 index 05e19fd86..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.14.kt +++ /dev/null @@ -1,6 +0,0 @@ -fun main() { -//sampleStart - val s = "abc" - println("$s.length is ${s.length}") // prints "abc.length is 3" -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.15.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.15.kt deleted file mode 100644 index 2800fbc61..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.15.kt +++ /dev/null @@ -1,7 +0,0 @@ -fun main() { -//sampleStart - // Creates an Array with values ["0", "1", "4", "9", "16"] - val asc = Array(5) { i -> (i * i).toString() } - asc.forEach { println(it) } -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.2.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.2.kt deleted file mode 100644 index cb68919a9..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.2.kt +++ /dev/null @@ -1,9 +0,0 @@ -fun main() { -//sampleStart - val b: Int = 10000 - println(b == b) // Prints 'true' - val boxedB: Int? = b - val anotherBoxedB: Int? = b - println(boxedB == anotherBoxedB) // Prints 'true' -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.3.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.3.kt deleted file mode 100644 index e172907c4..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.3.kt +++ /dev/null @@ -1,7 +0,0 @@ -fun main() { -//sampleStart - val b: Byte = 1 // OK, literals are checked statically - // val i: Int = b // ERROR - val i1: Int = b.toInt() -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.4.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.4.kt deleted file mode 100644 index 33031c01d..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.4.kt +++ /dev/null @@ -1,8 +0,0 @@ -fun main() { -//sampleStart - println(1 + 2) - println(2_500_000_000L - 1L) - println(3.14 * 2.71) - println(10.0 / 3) -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.5.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.5.kt deleted file mode 100644 index b1b733f07..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.5.kt +++ /dev/null @@ -1,7 +0,0 @@ -fun main() { -//sampleStart - val x = 5 / 2 - //println(x == 2.5) // ERROR: Operator '==' cannot be applied to 'Int' and 'Double' - println(x == 2) -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.6.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.6.kt deleted file mode 100644 index 5a3782fdd..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.6.kt +++ /dev/null @@ -1,6 +0,0 @@ -fun main() { -//sampleStart - val x = 5L / 2 - println(x == 2L) -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.7.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.7.kt deleted file mode 100644 index 16e0201c3..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.7.kt +++ /dev/null @@ -1,6 +0,0 @@ -fun main() { -//sampleStart - val x = 5 / 2.toDouble() - println(x == 2.5) -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.8.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.8.kt deleted file mode 100644 index 04c96d807..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.8.kt +++ /dev/null @@ -1,11 +0,0 @@ -fun main() { -//sampleStart - val myTrue: Boolean = true - val myFalse: Boolean = false - val boolNull: Boolean? = null - - println(myTrue || myFalse) - println(myTrue && myFalse) - println(!myTrue) -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.9.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.9.kt deleted file mode 100644 index 272e33cd0..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-types/be5678589cf16c80d1cda9c85d9a55d9.9.kt +++ /dev/null @@ -1,9 +0,0 @@ -fun main() { -//sampleStart - val aChar: Char = 'a' - - println(aChar) - println('\n') //prints an extra newline character - println('\uFF00') -//sampleEnd -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.1.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.1.kt index 342555a69..f0451009a 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.1.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.1.kt @@ -1,15 +1,14 @@ -fun main() { +fun main() { //sampleStart - open class Shape - class Rectangle: Shape() - - fun Shape.getName() = "Shape" - fun Rectangle.getName() = "Rectangle" - - fun printClassName(s: Shape) { - println(s.getName()) - } - - printClassName(Rectangle()) -//sampleEnd -} \ No newline at end of file + // builder is an instance of StringBuilder + val builder = StringBuilder() + // Calls .appendLine() extension function on builder + .appendLine("Hello") + .appendLine() + .appendLine("World") + println(builder.toString()) + // Hello + // + // World +} +//sampleEnd \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.10.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.10.kt new file mode 100644 index 000000000..882f7991a --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.10.kt @@ -0,0 +1,10 @@ +fun main() { + //sampleStart + data class Order(val weight: Double) + val calculateShipping = fun Order.(rate: Double): Double = this.weight * rate + + val order = Order(2.5) + val cost = order.calculateShipping(3.0) + println("Shipping cost: $cost") + // Shipping cost: 7.5 +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.11.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.11.kt new file mode 100644 index 000000000..99b985c8e --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.11.kt @@ -0,0 +1,8 @@ +fun main() { + val isInRange: Int.(min: Int, max: Int) -> Boolean = { min, max -> this in min..max } + + println(5.isInRange(1, 10)) + // true + println(20.isInRange(1, 10)) + // false +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.12.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.12.kt new file mode 100644 index 000000000..f3bba5aec --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.12.kt @@ -0,0 +1,12 @@ +data class User(val firstName: String, val lastName: String) + +// An extension property to get a username-style email handle +val User.emailUsername: String + get() = "${firstName.lowercase()}.${lastName.lowercase()}" + +fun main() { + val user = User("Mickey", "Mouse") + // Calls extension property + println("Generated email username: ${user.emailUsername}") + // Generated email username: mickey.mouse +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.13.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.13.kt new file mode 100644 index 000000000..6ccbd9066 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.13.kt @@ -0,0 +1,29 @@ +data class House(val streetName: String) + +// Doesn't compile because there is no getter and setter +// var House.number = 1 +// Error: Initializers are not allowed for extension properties + +// Compiles successfully +val houseNumbers = mutableMapOf() +var House.number: Int + get() = houseNumbers[this] ?: 1 + set(value) { + println("Setting house number for ${this.streetName} to $value") + houseNumbers[this] = value + } + +fun main() { + val house = House("Maple Street") + + // Shows the default + println("Default number: ${house.number} ${house.streetName}") + // Default number: 1 Maple Street + + house.number = 99 + // Setting house number for Maple Street to 99 + + // Shows the updated number + println("Updated number: ${house.number} ${house.streetName}") + // Updated number: 99 Maple Street +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.14.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.14.kt new file mode 100644 index 000000000..5c89ee901 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.14.kt @@ -0,0 +1,12 @@ +class Logger { + companion object { } +} + +fun Logger.Companion.logStartupMessage() { + println("Application started.") +} + +fun main() { + Logger.logStartupMessage() + // Application started. +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.15.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.15.kt new file mode 100644 index 000000000..654eed807 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.15.kt @@ -0,0 +1,32 @@ +class Host(val hostname: String) { + fun printHostname() { print(hostname) } +} + +class Connection(val host: Host, val port: Int) { + fun printPort() { print(port) } + + // Host is the extension receiver + fun Host.printConnectionString() { + // Calls Host.printHostname() + printHostname() + print(":") + // Calls Connection.printPort() + // Connection is the dispatch receiver + printPort() + } + + fun connect() { + /*...*/ + // Calls the extension function + host.printConnectionString() + } +} + +fun main() { + Connection(Host("kotl.in"), 443).connect() + // kotl.in:443 + + // Triggers an error because the extension function isn't available outside Connection + // Host("kotl.in").printConnectionString() + // Unresolved reference 'printConnectionString'. +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.16.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.16.kt new file mode 100644 index 000000000..a48768481 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.16.kt @@ -0,0 +1,48 @@ +open class User + +class Admin : User() + +open class NotificationSender { + open fun User.sendNotification() { + println("Sending user notification from normal sender") + } + + open fun Admin.sendNotification() { + println("Sending admin notification from normal sender") + } + + fun notify(user: User) { + user.sendNotification() + } +} + +class SpecialNotificationSender : NotificationSender() { + override fun User.sendNotification() { + println("Sending user notification from special sender") + } + + override fun Admin.sendNotification() { + println("Sending admin notification from special sender") + } +} + +fun main() { + // Dispatch receiver is NotificationSender + // Extension receiver is User + // Resolves to User.sendNotification() in NotificationSender + NotificationSender().notify(User()) + // Sending user notification from normal sender + + // Dispatch receiver is SpecialNotificationSender + // Extension receiver is User + // Resolves to User.sendNotification() in SpecialNotificationSender + SpecialNotificationSender().notify(User()) + // Sending user notification from special sender + + // Dispatch receiver is SpecialNotificationSender + // Extension receiver is User NOT Admin + // The notify() function declares user as type User + // Statically resolves to User.sendNotification() in SpecialNotificationSender + SpecialNotificationSender().notify(Admin()) + // Sending user notification from special sender +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.17.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.17.kt new file mode 100644 index 000000000..0a35d1e7f --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.17.kt @@ -0,0 +1,20 @@ +// File: StringUtils.kt + +private fun removeWhitespace(input: String): String { + return input.replace("\\s".toRegex(), "") +} + +fun String.cleaned(): String { + return removeWhitespace(this) +} + +fun main() { + val rawEmail = " user @example. com " + val cleaned = rawEmail.cleaned() + println("Raw: '$rawEmail'") + // Raw: ' user @example. com ' + println("Cleaned: '$cleaned'") + // Cleaned: 'user@example.com' + println("Looks like an email: ${cleaned.contains("@") && cleaned.contains(".")}") + // Looks like an email: true +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.18.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.18.kt new file mode 100644 index 000000000..2a36a986d --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.18.kt @@ -0,0 +1,19 @@ +class User(private val password: String) { + fun isLoggedIn(): Boolean = true + fun passwordLength(): Int = password.length +} + +// Extension declared outside the class +fun User.isSecure(): Boolean { + // Can't access password because it's private: + // return password.length >= 8 + + // Instead, we rely on public members: + return passwordLength() >= 8 && isLoggedIn() +} + +fun main() { + val user = User("supersecret") + println("Is user secure: ${user.isSecure()}") + // Is user secure: true +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.2.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.2.kt index a478042e0..2b2395054 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.2.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.2.kt @@ -1,11 +1,13 @@ +fun String.truncate(maxLength: Int): String { + return if (this.length <= maxLength) this else take(maxLength - 3) + "..." +} + fun main() { -//sampleStart - class Example { - fun printFunctionType() { println("Class method") } - } - - fun Example.printFunctionType() { println("Extension function") } - - Example().printFunctionType() -//sampleEnd + val shortUsername = "KotlinFan42" + val longUsername = "JetBrainsLoverForever" + + println("Short username: ${shortUsername.truncate(15)}") + // KotlinFan42 + println("Long username: ${longUsername.truncate(15)}") + // JetBrainsLov... } \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.3.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.3.kt index e08257e40..e60dea737 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.3.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.3.kt @@ -1,11 +1,15 @@ +interface User { + val name: String + val email: String +} + +fun User.displayInfo(): String = "User(name=$name, email=$email)" + +// Inherits from and implements the properties of the User interface +class RegularUser(override val name: String, override val email: String) : User + fun main() { -//sampleStart - class Example { - fun printFunctionType() { println("Class method") } - } - - fun Example.printFunctionType(i: Int) { println("Extension function #$i") } - - Example().printFunctionType(1) -//sampleEnd + val user = RegularUser("Alice", "alice@example.com") + println(user.displayInfo()) + // User(name=Alice, email=alice@example.com) } \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.4.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.4.kt index 0252a3ece..b4d06877b 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.4.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.4.kt @@ -1,9 +1,14 @@ -class MyClass { - companion object { } // will be called "Companion" +fun Map.mostVoted(): String? { + return maxByOrNull { (key, value) -> value }?.key } -fun MyClass.Companion.printCompanion() { println("companion") } - fun main() { - MyClass.printCompanion() + val poll = mapOf( + "Cats" to 37, + "Dogs" to 58, + "Birds" to 22 + ) + + println("Top choice: ${poll.mostVoted()}") + // Dogs } \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.5.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.5.kt index 33933bde6..851f8dcb7 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.5.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.5.kt @@ -1,23 +1,16 @@ -class Host(val hostname: String) { - fun printHostname() { print(hostname) } +fun List.endpoints(): Pair { + return first() to last() } -class Connection(val host: Host, val port: Int) { - fun printPort() { print(port) } - - fun Host.printConnectionString() { - printHostname() // calls Host.printHostname() - print(":") - printPort() // calls Connection.printPort() - } +fun main() { + val cities = listOf("Paris", "London", "Berlin", "Prague") + val temperatures = listOf(21.0, 19.5, 22.3) - fun connect() { - /*...*/ - host.printConnectionString() // calls the extension function - } -} + val cityEndpoints = cities.endpoints() + val tempEndpoints = temperatures.endpoints() -fun main() { - Connection(Host("kotl.in"), 443).connect() - //Host("kotl.in").printConnectionString() // error, the extension function is unavailable outside Connection + println("First and last cities: $cityEndpoints") + // (Paris, Prague) + println("First and last temperatures: $tempEndpoints") + // (21.0, 22.3) } \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.6.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.6.kt index 5dbc58e61..122d36319 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.6.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.6.kt @@ -1,33 +1,19 @@ -open class Base { } - -class Derived : Base() { } - -open class BaseCaller { - open fun Base.printFunctionInfo() { - println("Base extension function in BaseCaller") - } - - open fun Derived.printFunctionInfo() { - println("Derived extension function in BaseCaller") - } - - fun call(b: Base) { - b.printFunctionInfo() // call the extension function - } -} - -class DerivedCaller: BaseCaller() { - override fun Base.printFunctionInfo() { - println("Base extension function in DerivedCaller") - } - - override fun Derived.printFunctionInfo() { - println("Derived extension function in DerivedCaller") - } -} - fun main() { - BaseCaller().call(Base()) // "Base extension function in BaseCaller" - DerivedCaller().call(Base()) // "Base extension function in DerivedCaller" - dispatch receiver is resolved virtually - DerivedCaller().call(Derived()) // "Base extension function in DerivedCaller" - extension receiver is resolved statically + //sampleStart + // Extension function on nullable Any + fun Any?.toString(): String { + if (this == null) return "null" + // After null check, `this` is smart-cast to non-nullable Any + // So this call resolves to the regular toString() function + return toString() + } + + val number: Int? = 42 + val nothing: Any? = null + + println(number.toString()) + // 42 + println(nothing.toString()) + // null + //sampleEnd } \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.7.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.7.kt new file mode 100644 index 000000000..135329102 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.7.kt @@ -0,0 +1,16 @@ +fun main() { +//sampleStart + open class Shape + class Rectangle: Shape() + + fun Shape.getName() = "Shape" + fun Rectangle.getName() = "Rectangle" + + fun printClassName(shape: Shape) { + println(shape.getName()) + } + + printClassName(Rectangle()) + // Shape +//sampleEnd +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.8.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.8.kt new file mode 100644 index 000000000..1285d00a6 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.8.kt @@ -0,0 +1,12 @@ +fun main() { +//sampleStart + class Example { + fun printFunctionType() { println("Member function") } + } + + fun Example.printFunctionType() { println("Extension function") } + + Example().printFunctionType() + // Member function +//sampleEnd +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.9.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.9.kt new file mode 100644 index 000000000..de510ff4e --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/extensions/433cd94fef7d8fc09ee6e9ca8379b874.9.kt @@ -0,0 +1,13 @@ +fun main() { +//sampleStart + class Example { + fun printFunctionType() { println("Member function") } + } + + // Same name but different signature + fun Example.printFunctionType(index: Int) { println("Extension function #$index") } + + Example().printFunctionType(1) + // Extension function #1 +//sampleEnd +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/fun-interfaces/6c48fe77f81f41caaaf4ef9b586c27b9.1.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/fun-interfaces/6c48fe77f81f41caaaf4ef9b586c27b9.1.kt index 27f8d4439..e5124b676 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/fun-interfaces/6c48fe77f81f41caaaf4ef9b586c27b9.1.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/fun-interfaces/6c48fe77f81f41caaaf4ef9b586c27b9.1.kt @@ -1,9 +1,9 @@ fun interface IntPredicate { - fun accept(i: Int): Boolean + fun accept(i: Int): Boolean } val isEven = IntPredicate { it % 2 == 0 } fun main() { - println("Is 7 even? - ${isEven.accept(7)}") + println("Is 7 even? - ${isEven.accept(7)}") } \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.1.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.1.kt new file mode 100644 index 000000000..a783d3b9b --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.1.kt @@ -0,0 +1,15 @@ +fun main () { +//sampleStart +fun greeting( + userId: Int = 0, + message: () -> Unit, +) +{ println(userId) + message() } + +// Uses the default value for 'userId' +greeting() { println ("Hello!") } +// 0 +// Hello! +//sampleEnd +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.2.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.2.kt new file mode 100644 index 000000000..9e795ec29 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.2.kt @@ -0,0 +1,13 @@ +fun main() { +//sampleStart + fun read( + b: Int, + print: Unit? = println("No argument passed for 'print'") + ) { println(b) } + + // Prints "No argument passed for 'print'", then "1" + read(1) + // Prints only "1" + read(1, null) + //sampleEnd +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.3.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.3.kt new file mode 100644 index 000000000..02f9066ce --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.3.kt @@ -0,0 +1,20 @@ +fun main() { + //sampleStart + fun log( + level: Int = 0, + code: Int = 1, + action: () -> Unit, + ) { println (level) + println (code) + action() } + + // Passes 1 for 'level' and uses the default value 1 for 'code' + log(1) { println("Connection established") } + + // Uses both default values, 0 for 'level' and 1 for 'code' + log(action = { println("Connection established") }) + + // Equivalent to the previous call, uses both default values + log { println("Connection established") } + //sampleEnd +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.4.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.4.kt new file mode 100644 index 000000000..8d78fc979 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.4.kt @@ -0,0 +1,27 @@ +class MyStringCollection { + val items = mutableListOf() + + infix fun add(s: String) { + println("Adding: $s") + items += s + } + + fun build() { + add("first") // Correct: ordinary function call + this add "second" // Correct: infix call with an explicit receiver + // add "third" // Compiler error: needs an explicit receiver + } + + fun printAll() = println("Items = $items") +} + +fun main() { + val myStrings = MyStringCollection() + // Adds "first" and "second" to the list twice + myStrings.build() + + myStrings.printAll() + // Adding: first + // Adding: second + // Items = [first, second] +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.5.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.5.kt new file mode 100644 index 000000000..f684499b8 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.5.kt @@ -0,0 +1,25 @@ +class Person(val name: String) { + val friends = mutableListOf() +} +class SocialGraph(val people: List) +//sampleStart +fun dfs(graph: SocialGraph) { + fun dfs(current: Person, visited: MutableSet) { + if (!visited.add(current)) return + println("Visited ${current.name}") + for (friend in current.friends) + dfs(friend, visited) + } + dfs(graph.people[0], HashSet()) +} +//sampleEnd +fun main() { + val alice = Person("Alice") + val bob = Person("Bob") + val charlie = Person("Charlie") + alice.friends += bob + bob.friends += charlie + charlie.friends += alice + val network = SocialGraph(listOf(alice, bob, charlie)) + dfs(network) +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.6.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.6.kt new file mode 100644 index 000000000..9b8167556 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/functions/0e665030c5b12d50d2cadbb175fb3d8c.6.kt @@ -0,0 +1,26 @@ +class Person(val name: String) { + val friends = mutableListOf() +} +class SocialGraph(val people: List) +//sampleStart +fun dfs(graph: SocialGraph) { + val visited = HashSet() + fun dfs(current: Person) { + if (!visited.add(current)) return + println("Visited ${current.name}") + for (friend in current.friends) + dfs(friend) + } + dfs(graph.people[0]) +} +//sampleEnd +fun main() { + val alice = Person("Alice") + val bob = Person("Bob") + val charlie = Person("Charlie") + alice.friends += bob + bob.friends += charlie + charlie.friends += alice + val network = SocialGraph(listOf(alice, bob, charlie)) + dfs(network) +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.3.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.3.kt index dc0ffccbc..12c9f56cc 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.3.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.3.kt @@ -2,7 +2,10 @@ class Client() { var token: String? = null fun connect() = println("connected!") fun authenticate() = println("authenticated!") - fun getData(): String = "Mock data" + fun getData() : String { + println("getting data!") + return "Mock data" + } } val client = Client() @@ -14,4 +17,5 @@ fun main() { client.authenticate() // authenticated! client.getData() + // getting data! } \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.4.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.4.kt index 9be33babf..ca2ed22fa 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.4.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.4.kt @@ -2,18 +2,22 @@ class Client() { var token: String? = null fun connect() = println("connected!") fun authenticate() = println("authenticated!") - fun getData(): String = "Mock data" + fun getData() : String { + println("getting data!") + return "Mock data" + } } //sampleStart val client = Client().apply { token = "asdf" connect() + // connected! authenticate() + // authenticated! } fun main() { client.getData() - // connected! - // authenticated! + // getting data! } //sampleEnd \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.5.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.5.kt index 923fdccdf..e06de0466 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.5.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/kotlin-tour-intermediate-scope-functions/5ddc626d44be3297d9e5c8e32c63fa4c.5.kt @@ -2,7 +2,10 @@ class Client() { var token: String? = null fun connect() = println("connected!") fun authenticate() = println("authenticated!") - fun getData(): String = "Mock data" + fun getData() : String { + println("getting data!") + return "Mock data" + } } //sampleStart @@ -17,6 +20,7 @@ fun main() { authenticate() // authenticated! getData() + // getting data! } } //sampleEnd \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/numbers/f56d9449111795feee96b7f922b3320d.6.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/numbers/f56d9449111795feee96b7f922b3320d.6.kt index 1bb59406e..a11aee1b6 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/numbers/f56d9449111795feee96b7f922b3320d.6.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/numbers/f56d9449111795feee96b7f922b3320d.6.kt @@ -1,20 +1,12 @@ fun main() { - //sampleStart - // Operand statically typed as floating-point number - println(Double.NaN == Double.NaN) // false +//sampleStart + val x = 1 + val xShiftedLeft = (x shl 2) + println(xShiftedLeft) + // 4 - // Operand NOT statically typed as floating-point number - // So NaN is equal to itself - println(listOf(Double.NaN) == listOf(Double.NaN)) // true - - // Operand statically typed as floating-point number - println(0.0 == -0.0) // true - - // Operand NOT statically typed as floating-point number - // So -0.0 is less than 0.0 - println(listOf(0.0) == listOf(-0.0)) // false - - println(listOf(Double.NaN, Double.POSITIVE_INFINITY, 0.0, -0.0).sorted()) - // [-0.0, 0.0, Infinity, NaN] - //sampleEnd + val xAnd = x and 0x000FF000 + println(xAnd) + // 0 +//sampleEnd } \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/numbers/f56d9449111795feee96b7f922b3320d.7.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/numbers/f56d9449111795feee96b7f922b3320d.7.kt new file mode 100644 index 000000000..1bb59406e --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/numbers/f56d9449111795feee96b7f922b3320d.7.kt @@ -0,0 +1,20 @@ +fun main() { + //sampleStart + // Operand statically typed as floating-point number + println(Double.NaN == Double.NaN) // false + + // Operand NOT statically typed as floating-point number + // So NaN is equal to itself + println(listOf(Double.NaN) == listOf(Double.NaN)) // true + + // Operand statically typed as floating-point number + println(0.0 == -0.0) // true + + // Operand NOT statically typed as floating-point number + // So -0.0 is less than 0.0 + println(listOf(0.0) == listOf(-0.0)) // false + + println(listOf(Double.NaN, Double.POSITIVE_INFINITY, 0.0, -0.0).sorted()) + // [-0.0, 0.0, Infinity, NaN] + //sampleEnd +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.1.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.1.kt index 8fbc81d87..4f795b358 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.1.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.1.kt @@ -1,28 +1,15 @@ -//sampleStart -fun testString() { - var stringInput: String? = null - // stringInput is smart-cast to String type - stringInput = "" - try { - // The compiler knows that stringInput isn't null - println(stringInput.length) - // 0 +fun main() { + val input: Any = "Hello, Kotlin" - // The compiler rejects previous smart cast information for - // stringInput. Now stringInput has the String? type. - stringInput = null + if (input is String) { + println("Message length: ${input.length}") + // Message length: 13 + } - // Trigger an exception - if (2 > 1) throw Exception() - stringInput = "" - } catch (exception: Exception) { - // The compiler knows stringInput can be null - // so stringInput stays nullable. - println(stringInput?.length) - // null + if (input !is String) { // Same as !(input is String) + println("Input is not a valid message") + } else { + println("Processing message: ${input.length} characters") + // Processing message: 13 characters } -} -//sampleEnd -fun main() { - testString() } \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.10.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.10.kt new file mode 100644 index 000000000..f6f4271fc --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.10.kt @@ -0,0 +1,21 @@ +interface Animal { + fun makeSound() +} + +class Dog : Animal { + // Implements behavior for makeSound() + override fun makeSound() { + println("Dog says woof!") + } +} + +fun printAnimalInfo(animal: Animal) { + animal.makeSound() +} + +fun main() { + val dog = Dog() + // Upcasts Dog instance to Animal + printAnimalInfo(dog) + // Dog says woof! +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.11.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.11.kt new file mode 100644 index 000000000..3f9ce36c0 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.11.kt @@ -0,0 +1,26 @@ +interface Animal { + fun makeSound() +} + +class Dog : Animal { + override fun makeSound() { + println("Dog says woof!") + } + + fun bark() { + println("BARK!") + } +} + +fun main() { + // Creates animal as a Dog instance with Animal + // type + val animal: Animal = Dog() + + // Safely downcasts animal to Dog type + val dog: Dog? = animal as? Dog + + // Uses a safe call to call bark() if dog isn't null + dog?.bark() + // "BARK!" +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.2.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.2.kt new file mode 100644 index 000000000..d1417361c --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.2.kt @@ -0,0 +1,49 @@ +interface Animal { + val name: String + fun speak() +} + +class Dog(override val name: String) : Animal { + override fun speak() = println("$name says: Woof!") +} + +class Cat(override val name: String) : Animal { + override fun speak() = println("$name says: Meow!") +} +//sampleStart +fun handleAnimal(animal: Animal) { + println("Handling animal: ${animal.name}") + animal.speak() + + // Use is operator to check for subtypes + if (animal is Dog) { + println("Special care instructions: This is a dog.") + } else if (animal is Cat) { + println("Special care instructions: This is a cat.") + } +} +//sampleEnd +fun main() { + val pets: List = listOf( + Dog("Buddy"), + Cat("Whiskers"), + Dog("Rex") + ) + + for (pet in pets) { + handleAnimal(pet) + println("---") + } + // Handling animal: Buddy + // Buddy says: Woof! + // Special care instructions: This is a dog. + // --- + // Handling animal: Whiskers + // Whiskers says: Meow! + // Special care instructions: This is a cat. + // --- + // Handling animal: Rex + // Rex says: Woof! + // Special care instructions: This is a dog. + // --- +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.3.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.3.kt new file mode 100644 index 000000000..330654a17 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.3.kt @@ -0,0 +1,12 @@ +fun logMessage(data: Any) { + // data is automatically cast to String + if (data is String) { + println("Received text: ${data.length} characters") + } +} + +fun main() { + logMessage("Server started") + // Received text: 14 characters + logMessage(404) +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.4.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.4.kt new file mode 100644 index 000000000..06d61a8e3 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.4.kt @@ -0,0 +1,12 @@ +fun logMessage(data: Any) { + // data is automatically cast to String + if (data !is String) return + + println("Received text: ${data.length} characters") +} + +fun main() { + logMessage("User signed in") + // Received text: 14 characters + logMessage(true) +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.5.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.5.kt new file mode 100644 index 000000000..94736843f --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.5.kt @@ -0,0 +1,19 @@ +fun processInput(data: Any) { + when (data) { + // data is automatically cast to Int + is Int -> println("Log: Assigned new ID ${data + 1}") + // data is automatically cast to String + is String -> println("Log: Received message \"$data\"") + // data is automatically cast to IntArray + is IntArray -> println("Log: Processed scores, total = ${data.sum()}") + } +} + +fun main() { + processInput(1001) + // Log: Assigned new ID 1002 + processInput("System rebooted") + // Log: Received message "System rebooted" + processInput(intArrayOf(10, 20, 30)) + // Log: Processed scores, total = 60 +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.6.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.6.kt new file mode 100644 index 000000000..2814f2f53 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.6.kt @@ -0,0 +1,37 @@ +sealed interface Status +data class Ok(val currentRoom: String) : Status +data object Error : Status + +class RobotVacuum(val rooms: List) { + var index = 0 + + fun status(): Status = + if (index < rooms.size) Ok(rooms[index]) + else Error + + fun clean(): Status { + println("Finished cleaning ${rooms[index]}") + index++ + return status() + } +} + +fun main() { + //sampleStart + val robo = RobotVacuum(listOf("Living Room", "Kitchen", "Hallway")) + + var status: Status = robo.status() + while (status is Ok) { + // The compiler smart casts status to OK type, so the currentRoom + // property is accessible. + println("Cleaning ${status.currentRoom}...") + status = robo.clean() + } + // Cleaning Living Room... + // Finished cleaning Living Room + // Cleaning Kitchen... + // Finished cleaning Kitchen + // Cleaning Hallway... + // Finished cleaning Hallway + //sampleEnd +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.7.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.7.kt new file mode 100644 index 000000000..8fbc81d87 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.7.kt @@ -0,0 +1,28 @@ +//sampleStart +fun testString() { + var stringInput: String? = null + // stringInput is smart-cast to String type + stringInput = "" + try { + // The compiler knows that stringInput isn't null + println(stringInput.length) + // 0 + + // The compiler rejects previous smart cast information for + // stringInput. Now stringInput has the String? type. + stringInput = null + + // Trigger an exception + if (2 > 1) throw Exception() + stringInput = "" + } catch (exception: Exception) { + // The compiler knows stringInput can be null + // so stringInput stays nullable. + println(stringInput?.length) + // null + } +} +//sampleEnd +fun main() { + testString() +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.8.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.8.kt new file mode 100644 index 000000000..b2a4c9a6e --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.8.kt @@ -0,0 +1,13 @@ +fun main() { + val rawInput: Any = "user-1234" + + // Casts to String successfully + val userId = rawInput as? String + println("Logging in user with ID: $userId") + // Logging in user with ID: user-1234 + + // Assigns a null value to wrongCast + val wrongCast = rawInput as? Int + println("wrongCast contains: $wrongCast") + // wrongCast contains: null +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.9.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.9.kt new file mode 100644 index 000000000..20c63a344 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/typecasts/2a72485ade1536976a77040176bcf3a1.9.kt @@ -0,0 +1,27 @@ +fun main() { + val config: Map = mapOf( + "username" to "kodee", + "alias" to null, + "loginAttempts" to 3 + ) + + // Unsafely casts to a nullable String + val username: String? = config["username"] as String? + println("Username: $username") + // Username: kodee + + // Unsafely casts a null value to a nullable String + val alias: String? = config["alias"] as String? + println("Alias: $alias") + // Alias: null + + // Fails to cast to nullable String and throws ClassCastException + // val unsafeAttempts: String? = config["loginAttempts"] as String? + // println("Login attempts (unsafe): $unsafeAttempts") + // Exception in thread "main" java.lang.ClassCastException + + // Fails to cast to nullable String and returns null + val safeAttempts: String? = config["loginAttempts"] as? String + println("Login attempts (safe): $safeAttempts") + // Login attempts (safe): null +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew23/0e71aba149e9509c275d31f17457ff45.1.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew23/0e71aba149e9509c275d31f17457ff45.1.kt new file mode 100644 index 000000000..f0db73272 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew23/0e71aba149e9509c275d31f17457ff45.1.kt @@ -0,0 +1,23 @@ +import kotlin.uuid.ExperimentalUuidApi +import kotlin.uuid.Uuid + +@OptIn(ExperimentalUuidApi::class) +fun main() { + val valid = Uuid.parseOrNull("550e8400-e29b-41d4-a716-446655440000") + println(valid) + // 550e8400-e29b-41d4-a716-446655440000 + + val invalid = Uuid.parseOrNull("not-a-uuid") + println(invalid) + // null + + + val hexDashValid = Uuid.parseHexDashOrNull("550e8400-e29b-41d4-a716-446655440000") + println(hexDashValid) + // 550e8400-e29b-41d4-a716-446655440000 + + + val hexDashInvalid = Uuid.parseHexDashOrNull("550e8400e29b41d4a716446655440000") + println(hexDashInvalid) + // null +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew23/0e71aba149e9509c275d31f17457ff45.2.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew23/0e71aba149e9509c275d31f17457ff45.2.kt new file mode 100644 index 000000000..2acaf4fb4 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew23/0e71aba149e9509c275d31f17457ff45.2.kt @@ -0,0 +1,17 @@ +import kotlin.uuid.ExperimentalUuidApi +import kotlin.uuid.Uuid + +@OptIn(ExperimentalUuidApi::class) +fun main() { + // Generates a v4 UUID + val v4 = Uuid.generateV4() + println(v4) + + // Generates a v7 UUID + val v7 = Uuid.generateV7() + println(v7) + + // Generates a v4 UUID + val random = Uuid.random() + println(random) +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew23/0e71aba149e9509c275d31f17457ff45.3.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew23/0e71aba149e9509c275d31f17457ff45.3.kt new file mode 100644 index 000000000..7cb622e1d --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew23/0e71aba149e9509c275d31f17457ff45.3.kt @@ -0,0 +1,13 @@ +import kotlin.uuid.ExperimentalUuidApi +import kotlin.uuid.Uuid +import kotlin.time.ExperimentalTime +import kotlin.time.Instant + +@OptIn(ExperimentalUuidApi::class, ExperimentalTime::class) +fun main() { + val timestamp = Instant.fromEpochMilliseconds(1577836800000) // 2020-01-01T00:00:00Z + + // Generates a v7 UUID for the specified timestamp (without monotonicity guarantees) + val v7AtTimestamp = Uuid.generateV7NonMonotonicAt(timestamp) + println(v7AtTimestamp) +} \ No newline at end of file