mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
[Kotlin/en] add kotlin operator overloading
This commit is contained in:
parent
67f785f102
commit
9438e56ae9
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user