Skip to content

Instantly share code, notes, and snippets.

@davidmigloz
Last active April 20, 2021 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmigloz/2e4258f8de0de8d54b4b55c0786f20c8 to your computer and use it in GitHub Desktop.
Save davidmigloz/2e4258f8de0de8d54b4b55c0786f20c8 to your computer and use it in GitHub Desktop.
Dart vs Kotlin: operator overloading
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}]");
}
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