A question about terminology: Are all methods func...
# announcements
k
A question about terminology: Are all methods functions? Does a method have to be a function defined inside the original type definition, or does it include extension functions?
s
the tl;dr is that methods are functions that belong to classes
k
Which is why I asked about extension functions
s
you could call them extension functions or extension methods, without the “extension” qualifier it becomes much more vague
👍 1
I wouldn’t call them methods myself because of how the implementation works
k
I was thinking the same thing 😄
m
I believe the docs refer to them as extension functions. Don't think I've ever seen them referred to as extension methods.
👍 1
s
I also haven’t seen them referred to as such either, but my anecdotal experience is limited
if someone called them extension methods in a conversation I’d still know exactly what they meant, and could understand why they did so considering what usage looks like at the call site
c
“method” is usually not associated with Kotlin at all. Technically, you can have “methods”, but kotlin really encourages you to code with “functions”. You don’t tell an object to do something, you ask it to compute a result. Whether a function is inside a class or an extension, it’s best to think of it as simply having the instance as an implicit argument to a function that simply returns a result, rather than asking a specific object to change its state
👍 3
m
Some would argue that methods mutate state, and potentially don't return anything, whereas a function doesn't mutate state and always returns something. But for Kotlin, method/function seems to be used interchangeably when talking about a class. I suspect because Kotlin uses
fun
for definition, but a lot of us came from Java.
👍 2