diff options
author | Divay Prakash <divayprakash@users.noreply.github.com> | 2019-04-02 22:06:16 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-02 22:06:16 +0530 |
commit | 3844aa7c332abcf5d827ca1641766a6645a46aae (patch) | |
tree | 647431a50d76e245c2734b8c781cfd0f60167c2a | |
parent | 46cf7f35536e855d4060559c489d382507fd7a1b (diff) | |
parent | 25a08b5931009fac3e3748cda0b692f8b8436b44 (diff) |
Merge pull request #3501 from iluxonchik/refactor-kotlin-examples
[Kotlin/EN] Refactor kotlin examples
-rw-r--r-- | kotlin.html.markdown | 117 |
1 files changed, 53 insertions, 64 deletions
diff --git a/kotlin.html.markdown b/kotlin.html.markdown index 9bc8b420..9336d217 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. */ @@ -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) } ``` |