0

Does assigning a variable to a function call actually call the function then or just store it (so it's ready to be called)?

For example does:

val userData = GraphRequest.newMeRequest(
    accessToken,
    object : GraphRequest.GraphJSONObjectCallback {
        override fun onCompleted(`object`: JSONObject?, response: GraphResponse?) {
            Log.d(TAG, `object`.toString())
        }

    }
)

actually call GraphRequest.newMeRequest()?

2
  • 1
    It calls the function then - quite easy to try too... Commented May 30, 2019 at 7:53
  • Another example around how to call a function - programiz.com/kotlin-programming/functions. So, it calls function and saves result to userData. Commented May 30, 2019 at 7:57

2 Answers 2

6

Yes, that code first calls GraphRequest.newMeRequest(), and assigns its result to userData.

In general, any time you see parens after a function name (or lambda), that will call the function.

If you want a reference to the function without calling it (e.g. to call later, or to return or pass to another function), you can use GraphRequest::newMeRequest.  You'll then need to pass the relevant arguments when you call it.

More info here.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, that will call the GraphRequest.newMeRequest(..) method and assign the result to userData immediately. If you want the field to be initialized at a later time, but only when the field is first accessed, use a lazy delegate.

val userData by lazy { /* call method */ }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.