Last active
August 18, 2020 19:53
-
-
Save Audhil/7cad42524125422d224c31c05a425634 to your computer and use it in GitHub Desktop.
kotlin operator overloading - ref: https://kotlinlang.org/docs/reference/operator-overloading.html to find allowed operators to overload
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
fun main() { | |
val bluePen = Pen(inkColor = "Blue") | |
bluePen.showInkColor() | |
val blackPen = Pen(inkColor = "Black") | |
blackPen.showInkColor() | |
val blueBlackPen = bluePen + blackPen | |
blueBlackPen.showInkColor() | |
} | |
operator fun Pen.plus(otherPen: Pen):Pen{ | |
val ink = "$inkColor, ${otherPen.inkColor}" | |
return Pen(inkColor = ink) | |
} | |
data class Pen(val inkColor:String){ | |
fun showInkColor(){ println(inkColor)} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment