diff options
author | AlburIvan <albur.ivan@outlook.com> | 2018-10-28 03:00:26 -0400 |
---|---|---|
committer | AlburIvan <albur.ivan@outlook.com> | 2018-10-28 03:00:26 -0400 |
commit | df0a0fa2a6ea1b818e3c6aaa139f77fde41f0256 (patch) | |
tree | c912eebef5bf5bfd45d4c2b6c91f355157041de8 /kotlin.html.markdown | |
parent | 73b8cd9a39ab9b63dd5e2c1c123c363fd014753a (diff) | |
parent | 27fa7c50ce23def736a69711f827918acc726e37 (diff) |
Merge branch 'master' of github.com:adambard/learnxinyminutes-docs into matlab-es
Diffstat (limited to 'kotlin.html.markdown')
-rw-r--r-- | kotlin.html.markdown | 81 |
1 files changed, 77 insertions, 4 deletions
diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 0c787d7e..81242bac 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. @@ -370,10 +372,81 @@ 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 + +/* 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 * [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/) |