what's the difference between a function and a val...
# announcements
l
what's the difference between a function and a val storing a lambda ? why do we need both ?
s
I would hazard a guess that its much more efficient both space and speedwise to use regular methods
l
hmm that doesn't really answer my question
s
Which one?
l
you're implying that one is more performant than the other, how so ?
and do i understand correctly that by "space wise" you mean the amount of text in the code we have to write?
s
I mean memory usage
But why one is more performant than the other will depend on how you implement either
Ie. if you store the lambda in a field it will occupy memory for every object instance whereas method usually only take up memory once
l
ah yeah that makes sense
i like to think of functions as simply being syntactic sugar for storing a lambda in a variable, and I'd like to know how far exactly that is from the truth
I see there's a memory optimization for member functions, anything else?
s
@LastExceed that’s interesting point.
while this is not Kotlin, still maybe interesting
k
i like to think of functions as simply being syntactic sugar for storing a lambda in a variable, and I'd like to know how far exactly that is from the truth
It's more the opposite way. A lambda in a val is storing an object to a separate class with an
invoke
member function which contains the code for your lambda.
👆 1
s
I second that @Kroppeb
t
I think it all depends on the target platform. For the JVM and probably native, using regular functions is cheaper. But for JavaScript,
fun
and
lambda var
should be roughly equivalent
👍 1