Sobes.tech
Junior — Middle

Is it permissible to use double brackets when calling a function, for example SpecialFunction()()?

sobes.tech AI

Answer from AI

In the Java language (used in Android development), calling a function with double parentheses, for example SpecialFunction()(), is not directly supported because the result of the first call should be an object that is itself callable (for example, a functional interface or lambda).

If SpecialFunction() returns an object that implements a functional interface (such as Callable or Runnable), then you can call a method on this object, but the syntax SpecialFunction()() in Java is impossible.

In Kotlin, which is often used for Android, such a call is possible if SpecialFunction() returns a function:

fun SpecialFunction(): () -> Unit {
    return { println("Hello") }
}

SpecialFunction()() // call the returned function

Thus, double parentheses are permissible if the first call returns a function that can be immediately invoked. This is rarely encountered in Java, but common in Kotlin.