diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.31-workaround.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.31-workaround.kt deleted file mode 100644 index cf9c3f911..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.31-workaround.kt +++ /dev/null @@ -1,25 +0,0 @@ -//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) - val any = object : Any() { - override fun toString(): String { - return "Any" - } - } - 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.32-workaround.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.32-workaround.kt deleted file mode 100644 index ded1b4af3..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/basic-syntax/5ec261721162729866abc1e8f7293ef4.32-workaround.kt +++ /dev/null @@ -1,22 +0,0 @@ -//sampleStart -fun getStringLength(obj: Any): Int? { - if (obj !is String) return null - - // `obj` is automatically cast to `String` in this branch - return obj.length -} -//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) - val any = object : Any() { - override fun toString(): String { - return "Any" - } - } - printLength(listOf(any)) -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/object-declarations/8a72411880f54ee0a305b57ed6c6901d.2-workaround.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/object-declarations/8a72411880f54ee0a305b57ed6c6901d.2-workaround.kt deleted file mode 100644 index 9619df31e..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/object-declarations/8a72411880f54ee0a305b57ed6c6901d.2-workaround.kt +++ /dev/null @@ -1,12 +0,0 @@ -object MyObject { - override fun toString(): String { - val superString = super.toString() - // MyObject@hashcode - return superString.substringBefore('@') - } -} - -fun main() { - println(MyObject) - // MyObject -} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.1.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.1.kt index 03e054e65..33aef7325 100644 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.1.kt +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.1.kt @@ -1,10 +1,46 @@ +class Address { + var name: String = "Holmes, Sherlock" + var street: String = "Baker" + var city: String = "London" +} + +interface ContactInfo { + val email: String +} + +object Company { + var name: String = "Detective Inc." + val country: String = "UK" +} + +class PersonContact : ContactInfo { + override val email: String = "sherlock@example.com" +} + //sampleStart -class Rectangle(val width: Int, val height: Int) { - val area: Int // property type is optional since it can be inferred from the getter's return type - get() = this.width * this.height +fun copyAddress(address: Address): Address { + val result = Address() + // Accesses properties in the result instance + result.name = address.name + result.street = address.street + result.city = address.city + return result } -//sampleEnd + fun main() { - val rectangle = Rectangle(3, 4) - println("Width=${rectangle.width}, height=${rectangle.height}, area=${rectangle.area}") -} \ No newline at end of file + val sherlockAddress = Address() + val copy = copyAddress(sherlockAddress) + // Accesses properties in the copy instance + println("Copied address: ${copy.name}, ${copy.street}, ${copy.city}") + // Copied address: Holmes, Sherlock, Baker, London + + // Accesses properties in the Company object + println("Company: ${Company.name} in ${Company.country}") + // Company: Detective Inc. in UK + + val contact = PersonContact() + // Access properties in the contact instance + println("Email: ${contact.email}") + // Email: sherlock@email.com +} +//sampleEnd \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.2.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.2.kt new file mode 100644 index 000000000..030813f9e --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.2.kt @@ -0,0 +1,10 @@ +//sampleStart +class Rectangle(val width: Int, val height: Int) { + val area: Int + get() = this.width * this.height +} +//sampleEnd +fun main() { + val rectangle = Rectangle(3, 4) + println("Width=${rectangle.width}, height=${rectangle.height}, area=${rectangle.area}") +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.3.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.3.kt new file mode 100644 index 000000000..f57c49157 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.3.kt @@ -0,0 +1,19 @@ +class Point(var x: Int, var y: Int) { + var coordinates: String + get() = "$x,$y" + set(value) { + val parts = value.split(",") + x = parts[0].toInt() + y = parts[1].toInt() + } +} + +fun main() { + val location = Point(1, 2) + println(location.coordinates) + // 1,2 + + location.coordinates = "10,20" + println("${location.x}, ${location.y}") + // 10, 20 +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.4.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.4.kt new file mode 100644 index 000000000..03dbe8ac6 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.4.kt @@ -0,0 +1,30 @@ +class BankAccount(initialBalance: Int) { + var balance: Int = initialBalance + // Only the class can modify the balance + private set + + fun deposit(amount: Int) { + if (amount > 0) balance += amount + } + + fun withdraw(amount: Int) { + if (amount > 0 && amount <= balance) balance -= amount + } +} + +fun main() { + val account = BankAccount(100) + println("Initial balance: ${account.balance}") + // 100 + + account.deposit(50) + println("After deposit: ${account.balance}") + // 150 + + account.withdraw(70) + println("After withdrawal: ${account.balance}") + // 80 + + // account.balance = 1000 + // Error: cannot assign because setter is private +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.5.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.5.kt new file mode 100644 index 000000000..8485bcb30 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.5.kt @@ -0,0 +1,16 @@ +class Scoreboard { + var score: Int = 0 + set(value) { + field = value + // Adds logging when updating the value + println("Score updated to $field") + } +} + +fun main() { + val board = Scoreboard() + board.score = 10 + // Score updated to 10 + board.score = 20 + // Score updated to 20 +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.6.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.6.kt new file mode 100644 index 000000000..c85fb668c --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.6.kt @@ -0,0 +1,29 @@ +class ShoppingCart { + // Backing property + private val _items = mutableListOf() + + // Public read-only view + val items: List + get() = _items + + fun addItem(item: String) { + _items.add(item) + } + + fun removeItem(item: String) { + _items.remove(item) + } +} + +fun main() { + val cart = ShoppingCart() + cart.addItem("Apple") + cart.addItem("Banana") + + println(cart.items) + // [Apple, Banana] + + cart.removeItem("Apple") + println(cart.items) + // [Banana] +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.7.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.7.kt new file mode 100644 index 000000000..ea10e949d --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.7.kt @@ -0,0 +1,23 @@ +class Temperature { + // Backing property storing temperature in Celsius + private var _celsius: Double = 0.0 + + var celsius: Double + get() = _celsius + set(value) { _celsius = value } + + var fahrenheit: Double + get() = _celsius * 9 / 5 + 32 + set(value) { _celsius = (value - 32) * 5 / 9 } +} + +fun main() { + val temp = Temperature() + temp.celsius = 25.0 + println("${temp.celsius}°C = ${temp.fahrenheit}°F") + // 25.0°C = 77.0°F + + temp.fahrenheit = 212.0 + println("${temp.celsius}°C = ${temp.fahrenheit}°F") + // 100.0°C = 212.0°F +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.8.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.8.kt new file mode 100644 index 000000000..b2f0ae609 --- /dev/null +++ b/src/test/resources/test-compile-data/jvm/kotlin-web-site/properties/07dffd9b6f168601c7b90ede42e8771a.8.kt @@ -0,0 +1,22 @@ +class WeatherStation { + lateinit var latestReading: String + + fun printReading() { + // Checks whether the property is initialized + if (this::latestReading.isInitialized) { + println("Latest reading: $latestReading") + } else { + println("No reading available") + } + } +} + +fun main() { + val station = WeatherStation() + + station.printReading() + // No reading available + station.latestReading = "22°C, sunny" + station.printReading() + // Latest reading: 22°C, sunny +} \ No newline at end of file diff --git a/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew11/4c8b9a374d2df52b3a804a9b8e54ee2b.20-workaround.kt b/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew11/4c8b9a374d2df52b3a804a9b8e54ee2b.20-workaround.kt deleted file mode 100644 index 77343476c..000000000 --- a/src/test/resources/test-compile-data/jvm/kotlin-web-site/whatsnew11/4c8b9a374d2df52b3a804a9b8e54ee2b.20-workaround.kt +++ /dev/null @@ -1,7 +0,0 @@ -fun main(args: Array) { -//sampleStart - val array = arrayOf("a", "b", "c") - println(array.toString().substringBefore('@')) // JVM implementation: type-and-hash gibberish - println(array.contentToString()) // nicely formatted as list -//sampleEnd -} \ No newline at end of file