1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-05 14:27:51 +02:00

Merge pull request #3501 from iluxonchik/refactor-kotlin-examples

[Kotlin/EN] Refactor kotlin examples
This commit is contained in:
Divay Prakash
2019-04-02 22:06:16 +05:30
committed by GitHub

View File

@@ -109,7 +109,7 @@ fun helloWorld(val name : String) {
/* /*
When a function consists of a single expression then the curly brackets can 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 fun odd(x: Int): Boolean = x % 2 == 1
println(odd(6)) // => false println(odd(6)) // => false
@@ -306,7 +306,7 @@ fun helloWorld(val name : String) {
println(result) 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 If an object passes a type check then it can be used as that type without
explicitly casting it. explicitly casting it.
*/ */
@@ -346,11 +346,6 @@ fun helloWorld(val name : String) {
return this.filter {it != c} return this.filter {it != c}
} }
println("Hello, world!".remove('l')) // => Heo, word! println("Hello, world!".remove('l')) // => Heo, word!
println(EnumExample.A) // => A
println(ObjectExample.hello()) // => hello
testOperator()
} }
// Enum classes are similar to Java enum types. // Enum classes are similar to Java enum types.
@@ -358,6 +353,8 @@ enum class EnumExample {
A, B, C A, B, C
} }
fun printEnum() = println(EnumExample.A) // => A
/* /*
The "object" keyword can be used to create singleton objects. 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. We cannot instantiate it but we can refer to its unique instance by its name.
@@ -367,11 +364,18 @@ object ObjectExample {
fun hello(): String { fun hello(): String {
return "hello" return "hello"
} }
override fun toString(): String {
return "Hello, it's me, ${ObjectExample::class.simpleName}"
}
} }
fun useObject() {
ObjectExample.hello() fun useSingletonObject() {
val someRef: Any = ObjectExample // we use objects name just as is 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" var b: String? = "abc"
val l = b!!.length val l = b!!.length
/* You can add many custom operations using symbol like +, to particular instance data class Counter(var value: Int) {
by overloading the built-in kotlin operator, using "operator" keyword // overload Counter += Int
operator fun plusAssign(increment: Int) {
below is the sample class to add some operator, and the most basic example this.value += increment
*/
data class SomeClass(var savedValue: Int = 0)
// instance += valueToAdd
operator fun SomeClass.plusAssign(valueToAdd: Int) {
this.savedValue += valueToAdd
} }
// -instance // overload Counter++ and ++Counter
operator fun SomeClass.unaryMinus() = SomeClass(-this.savedValue) operator fun inc() = Counter(value + 1)
// ++instance or instance++ // overload Counter + Counter
operator fun SomeClass.inc() = SomeClass(this.savedValue + 1) operator fun plus(other: Counter) = Counter(this.value + other.value)
// instance * other // overload Counter * Counter
operator fun SomeClass.times(other: SomeClass) = operator fun times(other: Counter) = Counter(this.value * other.value)
SomeClass(this.savedValue * other.savedValue)
// an overload for multiply // overload Counter * Int
operator fun SomeClass.times(value: Int) = SomeClass(this.savedValue * value) operator fun times(value: Int) = Counter(this.value * value)
// other in instance // overload Counter in Counter
operator fun SomeClass.contains(other: SomeClass) = operator fun contains(other: Counter) = other.value == this.value
other.savedValue == this.savedValue
// instance[dummyIndex] = valueToSet // overload Counter[Int] = Int
operator fun SomeClass.set(dummyIndex: Int, valueToSet: Int) { operator fun set(index: Int, value: Int) {
this.savedValue = valueToSet + dummyIndex this.value = index + value
} }
// instance() // overload Counter instance invocation
operator fun SomeClass.invoke() { operator fun invoke() = println("The value of the counter is $value")
println("instance invoked by invoker")
} }
/* You can also overload operators through an extension methods */
// overload -Counter
operator fun Counter.unaryMinus() = Counter(-this.value)
/* return type must be Integer, fun operatorOverloadingDemo() {
so that, it can be translated to "returned value" compareTo 0 var counter1 = Counter(0)
var counter2 = Counter(5)
for equality (==,!=) using operator will violates overloading equals function, counter1 += 7
since it is already defined in Any class println(counter1) // => Counter(value=7)
*/ println(counter1 + counter2) // => Counter(value=12)
operator fun SomeClass.compareTo(other: SomeClass) = println(counter1 * counter2) // => Counter(value=35)
this.savedValue - other.savedValue println(counter2 * 2) // => Counter(value=10)
println(counter1 in Counter(5)) // => false
fun testOperator() { println(counter1 in Counter(7)) // => true
var x = SomeClass(4) counter1[26] = 10
println(counter1) // => Counter(value=36)
println(x) // => "SomeClass(savedValue=4)" counter1() // => The value of the counter is 36
x += 10 println(-counter2) // => Counter(value=-5)
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
} }
``` ```