Last active
April 20, 2021 08:50
-
-
Save davidmigloz/2e4258f8de0de8d54b4b55c0786f20c8 to your computer and use it in GitHub Desktop.
Dart vs Kotlin: operator overloading
This file contains hidden or 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 { | |
final int x; | |
final int y; | |
const Point(this.x, this.y); | |
Point operator +(Point other) { | |
return Point(x + other.x, y + other.y); | |
} | |
} | |
void main() { | |
final p1 = Point(1,1); | |
final p2 = Point(3,5); | |
final p3 = p1 + p2; | |
print("[${p3.x},${p3.y}]"); | |
} |
This file contains hidden or 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
data class Point( | |
val x: Int, | |
val y: Int | |
) { | |
operator fun plus(other: Point): Point { | |
return Point(x + other.x, y + other.y) | |
} | |
} | |
fun main() { | |
val p1 = Point(1,1) | |
val p2 = Point(3,5) | |
val p3 = p1 + p2 | |
println("[${p3.x},${p3.y}]") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment