Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Named parameters allow callers to explicitly map an argument to a function’s parameter by its declared identifier during invocation. This mechanism decouples the argument from its positional index within the function signature, enabling arbitrary ordering of arguments at the call site.

Syntax

The syntax utilizes the parameter name followed by the assignment operator (=) and the argument value.
fun initializeConnection(host: String, port: Int, secure: Boolean) { ... }

// Fully named invocation (order independent)
initializeConnection(port = 443, secure = true, host = "api.example.com")

Technical Mechanics and Compiler Rules

1. Mixing Positional and Named Arguments Kotlin allows the combination of positional and named arguments in a single invocation. As of Kotlin 1.4, a positional argument can follow a named argument strictly if the positional argument remains in its correct declared index relative to the function signature.
fun configure(timeout: Int, retries: Int, logLevel: String) { ... }

// Valid: Positional argument '5000' is in the correct 1st position
configure(5000, logLevel = "DEBUG", retries = 3)

// Valid (Kotlin 1.4+): Positional argument '"INFO"' is in the correct 3rd position
configure(timeout = 1000, 5, "INFO") 

// Compilation Error: Positional argument after named argument is out of index
// configure(timeout = 1000, logLevel = "DEBUG", 3) 
2. Interaction with Default Arguments Named parameters are the primary mechanism for skipping intermediate parameters that define default values. If a function signature contains multiple default parameters, named parameters allow the caller to supply values only for specific parameters further down the signature chain.
fun buildRequest(url: String, method: String = "GET", headers: Map<String, String> = emptyMap(), body: String? = null) { ... }

// Skips 'method' and 'headers', targeting 'body' directly
buildRequest("https://api.example.com", body = "{ \"key\": \"value\" }")
3. Variable Number of Arguments (vararg) When assigning a value to a vararg parameter using its name, the expected type is the underlying array type. As of Kotlin 1.4, an array can be assigned directly to the named vararg parameter without the spread operator (*). The spread operator remains valid but is no longer strictly required for named vararg assignments.
fun executeCommand(name: String, vararg flags: String) { ... }

// Valid (Kotlin 1.4+): Direct array assignment without the spread operator
executeCommand(name = "build", flags = arrayOf("--clean", "--stacktrace"))

// Valid: Using the spread operator (required prior to Kotlin 1.4)
executeCommand(name = "build", flags = *arrayOf("--clean", "--stacktrace"))
4. Java Interoperability Restrictions Named parameters are strictly a Kotlin compiler feature and cannot be used when invoking Java methods. Because standard Java bytecode (prior to Java 8, and often by default thereafter) does not preserve parameter names, the Kotlin compiler cannot reliably map named arguments to Java method signatures.
// Assuming a Java class: public class Utils { public static void format(String text, int width) {} }

// Compilation Error: Named arguments are not allowed for Java functions
// Utils.format(text = "Hello", width = 80); 
Master Kotlin with Deep Grasping Methodology!Learn More