where A and B are comcrete types, and A has a getSomeB() method, and A’s constructor performing some expensive computations. I’d like to use the function like this:
doSmth(A(), param1.GetSomeB)
More interestingly, I’d like that, when a function takes a lsmbda as a the last parameter, I can access the pther srguments of the function inside the body of the lambda.
myFunc(a=SomeObject()){ doSomethingWith(a) }
To achieve the same functionality, I need to save the fiest parameter as a value/variable before the function call. I’d like to be able to do it without that, since I may not need those values to outlive the function call, and for code terseness.
This reminds me of using val in when expressions (which I really like)
What about this syntax instead:
// Example functions (same as OP)
fun doSmth(param1: A, param2: B)
fun myFunc(a: Int, block: () -> Unit) { /* ... */ }
// "param1" is declared and used the parentheses
doSmth(val param1 = A(), param1.GetSomeB)
// "a" is declared and used the parentheses and trailing closure
myFunc(val a = SomeObject()) {
// a is in scope here.
}
IMO this would communicate the declaration of a named variable more clearly, and explicitly show the reader that a is intended to be used as a variable.
Thanks for the tip. Hiwever, wrapping the functionality in :? + apply (plus having to declare a as a lambda argument) smell like boilerplste to me, the kind of boilerplste Kotlin strives to reduce, thus the proposal.
Using val keyword would be fine, albeit a bot redundant still, since function arguments are essentially implicits vals already. And that’s wht var would’t be expected(/legal there.
@arocnies can we already use when(val x = sth) {} ? Or is it just another proposal?
This argument getting close to “religious war” if to count ?: a boilerplate code or not. I am not going there.
Just want to point that in terms of amount of code both mine and proposed looks very close in size.
However my main point was that it seams very niche use case applicable only if you have default value of the parameter, which calculated at the time of call AND used to provide values for multiple purposes. This smells bed design to me.
Please provide the real-life example where you need this to find other options.