<@U5G4AJ9HA> Top level extension functions are ext...
# random
r
@onionsoup Top level extension functions are extremely nice when dealing with third party code you can't modify.
o
ruckustboom: sorry i didn't really understand what you mean by that. so you have third party code, how does top level functions make it nicer?
a
Copy code
someThirdPartyObjectInstance.myExtensionMethod()
vs
Copy code
SomeThirdPartyObjectUtils.myStaticMethod(someThirdPartyObjectInstance)
o
Adam, I think you misunderstood my question. I am not talking about extension functions, i am talking about top level functions, meaning a function that you can write outside of a class, straight into a file. the Kotlin developers seem to really like this feature but i don't see how is it really better than just putting a static function in a class.
a
Ah.
r
If you define an extension function in the top level of your package, you can use if everywhere. If you define it in a class, you can only use it where that class is in scope.
a
Ahh, okay, I see that now.
That seems useful for things like
println
or
log
- things you'd use a static import for in Java.
r
Basically the advantage is code cleanliness/readability. They are functionally the exact same, one just looks better.
f
I find myself reaching for extensions extremely frequently when a concept is best expressed in terms of another object; but if it's too local, I'll keep it within the scope of a file for cleanliness. Still, it often makes that file's logic incredibly crisp.
👍 1
Same thing in C#, just C# takes a lot more work to set those extension functions up.