Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make extension method / property available globally

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?

like image 547
Joschua Avatar asked Mar 11 '23 16:03

Joschua


1 Answers

You cannot do that, because extension methods are resolved statically by the compiler.

Without an import, the compiler does not know about the extension.

like image 117
Ingo Kegel Avatar answered Mar 26 '23 02:03

Ingo Kegel