Skip to content

Instantly share code, notes, and snippets.

@renaudcerrato
Last active February 22, 2019 08:42
Show Gist options
  • Save renaudcerrato/85b0b06e02d96a6e1e9f315a1b6d8b33 to your computer and use it in GitHub Desktop.
Save renaudcerrato/85b0b06e02d96a6e1e9f315a1b6d8b33 to your computer and use it in GitHub Desktop.
Kotlin Operators Example
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