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

Alejandro Rios

02/22/2022, 12:49 PM
Hello, noob question here, under what conditions we need to use
MyFunction.invoke()
instead of
MyFunction()
?
j

Joffrey

02/22/2022, 12:51 PM
In some cases you have to use
invoke
, for instance if the function instance is nullable and you want to use the safe-call operator:
myFunction?.invoke()
. But otherwise in general I would say it's a matter of style/preference.
👍 2
👍🏼 1
a

Alejandro Rios

02/22/2022, 12:52 PM
Thanks @Joffrey
e

ephemient

02/22/2022, 2:57 PM
just the other day a case came up where
Foo()
resolves to the constructor and
Foo.invoke()
resolves to the
invoke
operator on
Foo.Companion
, https://kotlinlang.slack.com/archives/C0B8MA7FA/p1645273130231929
👍🏼 1
👍 2