https://kotlinlang.org logo
#getting-started
Title
# getting-started
h

Henrik Johansson

03/27/2023, 11:34 AM
As a relative new kotliner I still find when to use Extension functions vs methods not easy. I mean they both work right and obvious cases with third party libs are easy. Is there any glaring down sides to extensoin functions other than tighter coupling? I have to import the functions at call site which I don't when using a method? I get there might be a tiny performance boost from the static dispatch but surely the compiler could do the same dispatch in many cases for methods as well?
c

CLOVIS

03/27/2023, 12:09 PM
Downsides to extension functions: they cannot be inherited/overriden by children classes, so it's not possible for a child to provide a faster implementation
On the JVM, the performance difference between static dispatch and dynamic dispatch is essentially irrelevant, unless you're doing something very specific you probably shouldn't take it into account
Have you read this? If not, I recommend it → https://elizarov.medium.com/extension-oriented-design-13f4f27deaee
👍 1
h

Henrik Johansson

03/27/2023, 12:12 PM
Thx I will read it asap!
The article makes sense and we will try to to internalize it in our team. It's sound advice.
c

CLOVIS

03/27/2023, 1:00 PM
In case you didn't notice, the author is Roman Elizarov, the Project Lead for Kotlin. There's essentially no answer that's more authoritative than this 🙂 I highly recommend his blog posts in general, they're all very well written.
h

Henrik Johansson

03/28/2023, 7:30 AM
I did notice 🙂 I got the idea but I would like even more description of the do's-and-dont's. I mean you learn as you make mistakes but it's always faster to learn from someone else's experience
👍 1
4 Views