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