I have a reference to a functionthat needs a parameter.
fun foo(x: Int) = 2 * x
val f: KFunction1<Int, Int> = ::foo
Is there any way to write applyArgument
where
val f2: KFunction0<Int> = f1.applyArgument(42)
assertEquals("foo", f2.name)
assertEquals(84, f2())
I don't want to use a callable reference, as I need access to the name
property.
Partial application is possible.
You may just declare a function for partial application and use it for the
::
reference.Hence, the name would not be the original function. Another approach - create your own classes/interfaces
Now define the curring:
(it can be a member function too)
hope it helps you:
KFunction
s are intented to represent functions that are explicitly decleared in Kotlin code, butf2
is not declared anywhere in the code. In additionKFunction
has lot of reflection properties and functions which are not relevant to the applied functionf2
. Therefore even if it is possible it is not recommended.If you want to do it anyway you can simply write an
applyArgument
function in this way:But, if what you need is to preserve the name, I would do it in a safe way. One way could be:
Then use it: