are there any rules of thumb when to use extension...
# announcements
t
are there any rules of thumb when to use extension functions and when to put them into the type itself?
m
If you have control over the original, always put them in the original. Extension functions are designed to add to classes you can't modify directly.
1
d
I don't quite agree, utility/helper functions better not be a part of the interface
1
t
Another disagreement. Extension functions can be used to bound context in which a function is available
2
p
Another disagreement. You can use them to signal that a function is rather an utilitiy function on the class rather than the state itself. Read https://medium.com/@elizarov/extension-oriented-design-13f4f27deaee
t
wow thank you all. i guess this whole disagreement is a clear sign that we all might not have fully understood what they are good for 😄
i think in my context there are aspects of all your answers contained. the function i wanted to add ti an interface is not necessarily need by ALL implementors, but possibly many of them. it partly contains logic bound to a single context and finally the interface is declared in a modul i though i do have access to it i do not want to put much logic there 😄
m
As per others, the answer depends on the context. I should have thought a little longer before replying…