I'm confused about when to use extension functions...
# getting-started
t
I'm confused about when to use extension functions. Take this example in Ktor: https://github.com/Kotlin/ktor/blob/master/ktor-core/src/org/jetbrains/ktor/http/ContentTypes.kt It contains the ContentType class, but at line 120, this extension function is right next to the class:
Copy code
fun ContentType.withCharset(charset: Charset) = withParameter("charset", charset.name())
Why isn't it placed inside the class?
d
thisen: In this case it looks like utility methods that are just simple helpers for other methods in the class are implemented as extension functions, which is the approach I would take, too. Then you don't have to clutter your class with those helper methods.
t
But the extension can be used "outside" this .kt file, right?
d
Yes, you just need to import it like a class.
t
Great. I get the idea. Thank you for the quick reply. 🙂