Function named arguments should be also like local variables

Function named arguments can be reused in two use cases:

USE CASE 1:
There should be no need to create a temporary variable if we have named arguments. And we should be able to reuse them in function call.

foo(
    arg1 = 1,
    arg2 = 2,
    arg3 = arg1 + arg2,
)

USE CASE 2:
We can also continue to use named argument further after the function call as a read-only local variable.

foo(
    arg1 = 1,
    arg2 = 2,
    arg3 = arg1 + arg2,
)

val someVal = arg2@foo + arg3@foo

YouTrack issue: KT-52028

Function calls are for, well, calling of functions and not for defining variables. Case 1 makes some sense, because variable is created in a very local scope. Although, for consistency and for backward compatibility, it should probably use val, for example: foo(val arg1 = 1, ...). But I strongly disagree with case 2 where variable is created implicitly and outside of the function call. Also, what should be the value of arg2@foo if foo() was called multiple times?

For now I believe when is the only (?) exceptional case where we can define variables as a part of some language construct. But in when statement val is almost at the beginning of the line, so it is clearly visible. Variables created in function calls would be obscured.

2 Likes

Good point.
USE CASE 2 is just a try to get some thing more. I do not insist)

1 Like