From c1d773eab766891c8c9f5820bf18c8fafa6a8d5e Mon Sep 17 00:00:00 2001 From: Pablo Najt Date: Fri, 24 Mar 2017 14:53:41 -0300 Subject: [kotlin/all] Rename variable fooMutableDate to fooMutableData (#2688) (#2689) --- kotlin.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index d1f1aae6..a7f26126 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -189,13 +189,13 @@ fun helloWorld(val name : String) { // The "with" function is similar to the JavaScript "with" statement. data class MutableDataClassExample (var x: Int, var y: Int, var z: Int) - val fooMutableDate = MutableDataClassExample(7, 4, 9) - with (fooMutableDate) { + val fooMutableData = MutableDataClassExample(7, 4, 9) + with (fooMutableData) { x -= 2 y += 2 z-- } - println(fooMutableDate) // => MutableDataClassExample(x=5, y=6, z=8) + println(fooMutableData) // => MutableDataClassExample(x=5, y=6, z=8) /* We can create a list using the "listOf" function. -- cgit v1.2.3 From d30d6f69ca07014efdce95d771403a43423d2143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vitor=20Verona=20Biazibetti?= Date: Fri, 19 May 2017 18:34:27 -0300 Subject: [Kotlin/all] Remove whitespace in variable declaration with explicit type (#2733) * Removed whitespace in variable declaration with explicit type (Kotlin) * Removed whitespace in variable declaration with explicit type (Kotlin translations) --- kotlin.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index a7f26126..ca11ee3c 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -36,7 +36,7 @@ fun main(args: Array) { so we don't have to explicitly specify it every time. We can explicitly declare the type of a variable like so: */ - val foo : Int = 7 + val foo: Int = 7 /* Strings can be represented in a similar way as in Java. -- cgit v1.2.3 From 6350beb6e6549d5a1df51f8068e3dcf461758cff Mon Sep 17 00:00:00 2001 From: Bharat Reddy Date: Fri, 7 Jul 2017 11:26:29 +0100 Subject: Added print output for template string command --- kotlin.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index ca11ee3c..0c787d7e 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -65,7 +65,7 @@ fun helloWorld(val name : String) { A template expression starts with a dollar sign ($). */ val fooTemplateString = "$fooString has ${fooString.length} characters" - println(fooTemplateString) + println(fooTemplateString) // => My String Is Here! has 18 characters /* For a variable to hold null it must be explicitly specified as nullable. -- cgit v1.2.3 From 14719728ddbe8b71379c7702d3985f1c8b352e15 Mon Sep 17 00:00:00 2001 From: Zohar Jackson Date: Mon, 10 Sep 2018 11:44:00 -0400 Subject: Update kotlin.html.markdown --- kotlin.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 0c787d7e..86d1baa0 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -370,6 +370,12 @@ fun useObject() { val someRef: Any = ObjectExample // we use objects name just as is } + +/* The not-null assertion operator (!!) converts any value to a non-null type and +throws an exception if the value is null. +*/ +var b: String? = "abc" +val l = b!!.length ``` ### Further Reading -- cgit v1.2.3 From 9438e56ae90755582be35b6756e5b97dd5d1334a Mon Sep 17 00:00:00 2001 From: Muhammad Rifqi Fatchurrahman Date: Wed, 3 Oct 2018 00:56:58 +0700 Subject: [Kotlin/en] add kotlin operator overloading --- kotlin.html.markdown | 73 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 3 deletions(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 86d1baa0..6ff7c1dc 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -65,7 +65,7 @@ fun helloWorld(val name : String) { A template expression starts with a dollar sign ($). */ val fooTemplateString = "$fooString has ${fooString.length} characters" - println(fooTemplateString) // => My String Is Here! has 18 characters + println(fooTemplateString) // => My String Is Here! has 18 characters /* For a variable to hold null it must be explicitly specified as nullable. @@ -175,12 +175,12 @@ fun helloWorld(val name : String) { // Objects can be destructured into multiple variables. val (a, b, c) = fooCopy println("$a $b $c") // => 1 100 4 - + // destructuring in "for" loop for ((a, b, c) in listOf(fooData)) { println("$a $b $c") // => 1 100 4 } - + val mapData = mapOf("a" to 1, "b" to 2) // Map.Entry is destructurable as well for ((key, value) in mapData) { @@ -347,6 +347,8 @@ fun helloWorld(val name : String) { println(EnumExample.A) // => A println(ObjectExample.hello()) // => hello + + testOperator() } // Enum classes are similar to Java enum types. @@ -376,6 +378,71 @@ throws an exception if the value is null. */ var b: String? = "abc" val l = b!!.length + +/* You can add many custom operations using symbol like +, to particular instance +by overloading the built-in kotlin operator, using "operator" keyword + +below is the sample class to add some operator, and the most basic example +*/ +data class SomeClass(var savedValue: Int = 0) + +// instance += valueToAdd +operator fun SomeClass.plusAssign(valueToAdd: Int) { + this.savedValue += valueToAdd +} + +// -instance +operator fun SomeClass.unaryMinus() = SomeClass(-this.savedValue) + +// ++instance or instance++ +operator fun SomeClass.inc() = SomeClass(this.savedValue + 1) + +// instance * other +operator fun SomeClass.times(other: SomeClass) = + SomeClass(this.savedValue * other.savedValue) + +// an overload for multiply +operator fun SomeClass.times(value: Int) = SomeClass(this.savedValue * value) + +// other in instance +operator fun SomeClass.contains(other: SomeClass) = + other.savedValue == this.savedValue + +// instance[dummyIndex] = valueToSet +operator fun SomeClass.set(dummyIndex: Int, valueToSet: Int) { + this.savedValue = valueToSet + dummyIndex +} + +// instance() +operator fun SomeClass.invoke() { + println("instance invoked by invoker") +} + +/* return type must be Integer, +so that, it can be translated to "returned value" compareTo 0 + +for equality (==,!=) using operator will violates overloading equals function, +since it is already defined in Any class +*/ +operator fun SomeClass.compareTo(other: SomeClass) = + this.savedValue - other.savedValue + +fun testOperator() { + var x = SomeClass(4) + + println(x) // => "SomeClass(savedValue=4)" + x += 10 + println(x) // => "SomeClass(savedValue=14)" + println(-x) // => "SomeClass(savedValue=-14)" + println(++x) // => "SomeClass(savedValue=15)" + println(x * SomeClass(3)) // => "SomeClass(savedValue=45)" + println(x * 2) // => "SomeClass(savedValue=30)" + println(SomeClass(15) in x) // => true + x[2] = 10 + println(x) // => "SomeClass(savedValue=12)" + x() // => "instance invoked by invoker" + println(x >= 15) // => false +} ``` ### Further Reading -- cgit v1.2.3 From b3d01a51a86ef38e613d464d1e328502e8e728c7 Mon Sep 17 00:00:00 2001 From: Saurabh Sandav Date: Sat, 27 Oct 2018 11:24:10 +0530 Subject: Update link to run Kotlin in the browser --- kotlin.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 6ff7c1dc..81242bac 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -448,5 +448,5 @@ fun testOperator() { ### Further Reading * [Kotlin tutorials](https://kotlinlang.org/docs/tutorials/) -* [Try Kotlin in your browser](http://try.kotlinlang.org/) +* [Try Kotlin in your browser](https://play.kotlinlang.org/) * [A list of Kotlin resources](http://kotlin.link/) -- cgit v1.2.3 From 3aada35d9a938e1af78328c8ee170ac71846fb10 Mon Sep 17 00:00:00 2001 From: Henning Post Date: Wed, 31 Oct 2018 11:39:25 -0700 Subject: Mention parameterless main function --- kotlin.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 81242bac..9bc8b420 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -20,7 +20,9 @@ package com.learnxinyminutes.kotlin /* The entry point to a Kotlin program is a function named "main". -The function is passed an array containing any command line arguments. +The function is passed an array containing any command-line arguments. +Since Kotlin 1.3 the "main" function can also be defined without +any parameters. */ fun main(args: Array) { /* -- cgit v1.2.3 From f801ff3fe04d5a6f1ecd59d122d6e3ced4c5f71c Mon Sep 17 00:00:00 2001 From: Illya Gerasymchuk Date: Fri, 22 Mar 2019 17:44:10 +0000 Subject: fix grammar errors --- kotlin.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 9bc8b420..6a6c1b66 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -109,7 +109,7 @@ fun helloWorld(val name : String) { /* When a function consists of a single expression then the curly brackets can - be omitted. The body is specified after a = symbol. + be omitted. The body is specified after the = symbol. */ fun odd(x: Int): Boolean = x % 2 == 1 println(odd(6)) // => false @@ -306,7 +306,7 @@ fun helloWorld(val name : String) { println(result) /* - We can check if an object is a particular type by using the "is" operator. + We can check if an object is of a particular type by using the "is" operator. If an object passes a type check then it can be used as that type without explicitly casting it. */ -- cgit v1.2.3 From 25a08b5931009fac3e3748cda0b692f8b8436b44 Mon Sep 17 00:00:00 2001 From: Illya Gerasymchuk Date: Sat, 23 Mar 2019 13:31:06 +0000 Subject: refactor operator overloading example; reorder code Refactored the operator overloading example, fixed some grammar mistakes, reordered parts of the code to make it easier to read and some other minor fixes. --- kotlin.html.markdown | 113 +++++++++++++++++++++++---------------------------- 1 file changed, 51 insertions(+), 62 deletions(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 6a6c1b66..9336d217 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -346,11 +346,6 @@ fun helloWorld(val name : String) { return this.filter {it != c} } println("Hello, world!".remove('l')) // => Heo, word! - - println(EnumExample.A) // => A - println(ObjectExample.hello()) // => hello - - testOperator() } // Enum classes are similar to Java enum types. @@ -358,6 +353,8 @@ enum class EnumExample { A, B, C } +fun printEnum() = println(EnumExample.A) // => A + /* The "object" keyword can be used to create singleton objects. We cannot instantiate it but we can refer to its unique instance by its name. @@ -367,11 +364,18 @@ object ObjectExample { fun hello(): String { return "hello" } + + override fun toString(): String { + return "Hello, it's me, ${ObjectExample::class.simpleName}" + } } -fun useObject() { - ObjectExample.hello() - val someRef: Any = ObjectExample // we use objects name just as is + +fun useSingletonObject() { + println(ObjectExample.hello()) // => hello + // In Kotlin, "Any" is the root of the class hierarchy, just like "Object" is in Java + val someRef: Any = ObjectExample + println(someRef) // => Hello, it's me, ObjectExample } @@ -381,69 +385,54 @@ throws an exception if the value is null. var b: String? = "abc" val l = b!!.length -/* You can add many custom operations using symbol like +, to particular instance -by overloading the built-in kotlin operator, using "operator" keyword - -below is the sample class to add some operator, and the most basic example -*/ -data class SomeClass(var savedValue: Int = 0) +data class Counter(var value: Int) { + // overload Counter += Int + operator fun plusAssign(increment: Int) { + this.value += increment + } -// instance += valueToAdd -operator fun SomeClass.plusAssign(valueToAdd: Int) { - this.savedValue += valueToAdd -} + // overload Counter++ and ++Counter + operator fun inc() = Counter(value + 1) -// -instance -operator fun SomeClass.unaryMinus() = SomeClass(-this.savedValue) + // overload Counter + Counter + operator fun plus(other: Counter) = Counter(this.value + other.value) -// ++instance or instance++ -operator fun SomeClass.inc() = SomeClass(this.savedValue + 1) + // overload Counter * Counter + operator fun times(other: Counter) = Counter(this.value * other.value) -// instance * other -operator fun SomeClass.times(other: SomeClass) = - SomeClass(this.savedValue * other.savedValue) + // overload Counter * Int + operator fun times(value: Int) = Counter(this.value * value) -// an overload for multiply -operator fun SomeClass.times(value: Int) = SomeClass(this.savedValue * value) + // overload Counter in Counter + operator fun contains(other: Counter) = other.value == this.value -// other in instance -operator fun SomeClass.contains(other: SomeClass) = - other.savedValue == this.savedValue + // overload Counter[Int] = Int + operator fun set(index: Int, value: Int) { + this.value = index + value + } -// instance[dummyIndex] = valueToSet -operator fun SomeClass.set(dummyIndex: Int, valueToSet: Int) { - this.savedValue = valueToSet + dummyIndex -} + // overload Counter instance invocation + operator fun invoke() = println("The value of the counter is $value") -// instance() -operator fun SomeClass.invoke() { - println("instance invoked by invoker") } - -/* return type must be Integer, -so that, it can be translated to "returned value" compareTo 0 - -for equality (==,!=) using operator will violates overloading equals function, -since it is already defined in Any class -*/ -operator fun SomeClass.compareTo(other: SomeClass) = - this.savedValue - other.savedValue - -fun testOperator() { - var x = SomeClass(4) - - println(x) // => "SomeClass(savedValue=4)" - x += 10 - println(x) // => "SomeClass(savedValue=14)" - println(-x) // => "SomeClass(savedValue=-14)" - println(++x) // => "SomeClass(savedValue=15)" - println(x * SomeClass(3)) // => "SomeClass(savedValue=45)" - println(x * 2) // => "SomeClass(savedValue=30)" - println(SomeClass(15) in x) // => true - x[2] = 10 - println(x) // => "SomeClass(savedValue=12)" - x() // => "instance invoked by invoker" - println(x >= 15) // => false +/* You can also overload operators through an extension methods */ +// overload -Counter +operator fun Counter.unaryMinus() = Counter(-this.value) + +fun operatorOverloadingDemo() { + var counter1 = Counter(0) + var counter2 = Counter(5) + counter1 += 7 + println(counter1) // => Counter(value=7) + println(counter1 + counter2) // => Counter(value=12) + println(counter1 * counter2) // => Counter(value=35) + println(counter2 * 2) // => Counter(value=10) + println(counter1 in Counter(5)) // => false + println(counter1 in Counter(7)) // => true + counter1[26] = 10 + println(counter1) // => Counter(value=36) + counter1() // => The value of the counter is 36 + println(-counter2) // => Counter(value=-5) } ``` -- cgit v1.2.3 From 99bab9d9e9f8063b8e8d9a76393fbf782a5ec0da Mon Sep 17 00:00:00 2001 From: ashraf-patel Date: Mon, 5 Aug 2019 14:31:01 +0530 Subject: Kotlin enum --- kotlin.html.markdown | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 9336d217..5bbf6847 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -350,11 +350,22 @@ fun helloWorld(val name : String) { // Enum classes are similar to Java enum types. enum class EnumExample { - A, B, C + A, B, C // Enum constants are separated with commas. } - fun printEnum() = println(EnumExample.A) // => A +// Since each enum is an instance of the enum class, they can be initialized as: +enum class EnumExample(val value: Int) { + A(value = 1), + B(value = 2), + C(value = 3) +} +fun printProperty() = println(EnumExample.A.value) // => 1 + +// Every enum has properties to obtain its name and ordinal(position) in the enum class declaration: +fun printName() = println(EnumExample.A.name) // => A +fun printPosition() = println(EnumExample.A.ordinal) // => 0 + /* The "object" keyword can be used to create singleton objects. We cannot instantiate it but we can refer to its unique instance by its name. -- cgit v1.2.3 From e133062e353ae2e25daf50bb41f87f6896d2ae20 Mon Sep 17 00:00:00 2001 From: Alex P Date: Fri, 29 Jan 2021 20:23:05 +0000 Subject: fix: use fooData's values, not fooCopy's --- kotlin.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 5bbf6847..9394cc02 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -180,7 +180,7 @@ fun helloWorld(val name : String) { // destructuring in "for" loop for ((a, b, c) in listOf(fooData)) { - println("$a $b $c") // => 1 100 4 + println("$a $b $c") // => 1 2 4 } val mapData = mapOf("a" to 1, "b" to 2) -- cgit v1.2.3 From 2ff50ccd2c76ec8abda9e3bfc892c2a5cf64fe14 Mon Sep 17 00:00:00 2001 From: Tentacles <77739665+tentacleuno@users.noreply.github.com> Date: Thu, 4 Feb 2021 18:25:10 +0000 Subject: Update kotlin.html.markdown --- kotlin.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'kotlin.html.markdown') diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 9394cc02..12008074 100644 --- a/kotlin.html.markdown +++ b/kotlin.html.markdown @@ -426,7 +426,7 @@ data class Counter(var value: Int) { operator fun invoke() = println("The value of the counter is $value") } -/* You can also overload operators through an extension methods */ +/* You can also overload operators through extension methods */ // overload -Counter operator fun Counter.unaryMinus() = Counter(-this.value) -- cgit v1.2.3