In Kotlin you can define extension methods and properties for existing classes:
operator inline fun Vector2.plus(other: Vector2) = Vector2(x + other.x, y + other.y)
This allows one to do this:
val result = Vector2(1.1f, 2.3f) + Vector2(2f, 4f)
Is there any way I can make this extension global so that I don't have to import this in every class that uses this?
You cannot do that, because extension methods are resolved statically by the compiler.
Without an import, the compiler does not know about the extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With