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.

Positional parameters (or positional arguments) refer to the default mechanism of passing arguments to a function, method, or constructor where the mapping between the provided argument and the receiving parameter is determined strictly by their sequential order (index) in the function signature. The compiler binds the first argument to the first parameter, the second to the second, and so forth, evaluating them from left to right.

Syntax and Mechanics

When invoking a function using positional arguments, the types of the provided arguments must be assignable to (i.e., subtypes of) the declared parameter types at the corresponding indices.
fun initializeConnection(host: CharSequence, port: Number, secure: Boolean) {
    // Implementation
}

// Positional invocation (String is assignable to CharSequence, Int to Number)
initializeConnection("127.0.0.1", 443, true)
In the invocation above, the binding occurs via index mapping:
  • Index 0 ("127.0.0.1") binds to host: CharSequence
  • Index 1 (443) binds to port: Number
  • Index 2 (true) binds to secure: Boolean

Rules and Constraints

Strict Left-to-Right Binding Positional arguments are always consumed strictly from left to right. You cannot skip a parameter positionally. If a function defines default values for its parameters, positional arguments will overwrite those defaults in sequential order.
fun configure(timeout: Int = 30, retries: Int = 3, log: Boolean = false) { }

// Binds to `timeout` and `retries`. `log` retains its default value.
configure(60, 5) 
To skip timeout and retries and only provide a value for log, positional arguments cannot be used; named arguments are required. Mixing with Named Arguments Kotlin allows mixing positional and named arguments in a single invocation. When doing so, positional arguments must maintain their exact sequential alignment with the function signature. Prior to Kotlin 1.4, all positional arguments had to precede the first named argument. As of Kotlin 1.4+, a named argument can appear anywhere in the sequence, provided the subsequent positional arguments remain in their correct absolute index slots.
fun drawRectangle(x: Int, y: Int, width: Int, height: Int) { }

// Valid: Positional arguments precede named arguments
drawRectangle(10, 20, width = 100, height = 200)

// Valid (Kotlin 1.4+): Named arguments mixed with positional arguments.
// '20' is at index 1 (y), '100' is at index 2 (width). Both are in their correct absolute positions.
drawRectangle(x = 10, 20, 100, height = 200)

// Invalid: Positional argument '20' is placed at index 2. 
// Index 2 in the signature corresponds to 'width', not 'y'.
// drawRectangle(10, width = 100, 20, 200) 
Interaction with vararg When a function signature includes a vararg (variable number of arguments) parameter, positional arguments will be continuously absorbed by the vararg until the argument list ends or a named argument is encountered. If the vararg is not the final parameter in the signature, any parameters declared after it cannot be assigned using positional arguments.
fun executeCommand(vararg flags: String, command: String) { }

// Invalid: The compiler cannot determine where the vararg ends positionally.
// executeCommand("-v", "-f", "build") 

// Valid: Positional arguments fill the vararg, named argument terminates it.
executeCommand("-v", "-f", command = "build")
Master Kotlin with Deep Grasping Methodology!Learn More