When using kotlin extension function, is it possib...
# android
s
When using kotlin extension function, is it possible to initialize some value inside the extension function only once ( like does lazy work inside extension function) and not create instance every time i call extension function??
k
No.. Think of them as the old static Util classes you had in Java
s
Ok . Also one more question is using a lot of extension functions bad? As they ultimately are static functions under the hood. So does it affect memory usages or any other performance hits?
k
Think of it as the Java Utils classes.... That's essentially what they are..
s
Yah i know that . But my question still exists
k
Overuse I'd say yes...that means you're not designing classes correctly
I.e. why did you need the extension function when you could have added it to the class
s
To reuse the function in different classes and keep my class code clean .
g
Well, you can do this by introducing top mutable property, used by the extension function But usually it's not a good idea, it's shared mutable state, such code smells by definition, makes testing harder. But if you have a good case for it, it's very easy to do
s
Thanks i used lazy val inside another Helper Object class and accessed it from my extension function to meet my usecase
g
another Helper Object
You even don’t need another helper object, it can be a top level property