Last active
February 22, 2019 08:42
-
-
Save renaudcerrato/85b0b06e02d96a6e1e9f315a1b6d8b33 to your computer and use it in GitHub Desktop.
Kotlin Operators Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point(val x: Double, val y: Double) { | |
// addition operator | |
operator fun plus(other: Point): Point { | |
return Point(x + other.x, y + other.y) | |
} | |
// overload using Double | |
operator fun plus(value: Double): Point { | |
return Point(x + value, y + value) | |
} | |
} | |
// using extension function | |
operator fun Char.times(multiplier: Int): String { | |
return toString().repeat(multiplier) | |
} | |
print('!' * 5) // "!!!!!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment