Is it possible to create a placeholder function or...
# random
Is it possible to create a placeholder function or template (or anything else) that will be replaced by the result of its code later on? We need a temporary conversion function, let's say
fun f(x: Int) = x * 15 + 30
so if we type
in our code would be replaced with
later on (or even instantly). Not sure if it's ok to ask here, it seemed to me too random of a question to ask anywhere else. Edit: Even if it's possible somehow with the
annotation, let me know.
I don't think there is some feature like that in Kotlin, however, you could just have a normal function and might be able to inline it later, using the Intellij refactoring feature.
What's your use case?
What you’re looking for is Macro support, which Kotlin doesn’t have.
@louiscad Using measurements of a mockup design that are in pixels. I've determined how I want to translate them in DP, so I need a way of converting them on the fly as I create the layouts programmatically.
Honestly, making 2 integer operations is unlikely to slow your UI down. That said, if on Android or the JVM, I think R8 can inline all of that, essentially turning it into a constant.
☝️ 1
I know, it's just that there's no point of keeping the function in the code after finishing up with the mockup. (The function will make no sense to a future developer that has no access to the mockup anymore).
Then use the "Replace In Path" feature in the IDE (ctrl/cmd + shift + F).
What do you mean? How can you replace a function (which has a parameter) with its result like that?
you replace
by the actual known value
And do that for all values? 😝 Deprecating
and having it to be replaced automatically by the inner function (
x * 15 + 30
), I think it's a better solution.
So, what I did was to make a program that will evaluate the result of the function and replace the function call with it in all *.kt files in the specified dir (basically I automated Louis' answer). Thank you all for your help.