Is it possible to create a placeholder function or...
# random
r
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
f(10)
in our code would be replaced with
180
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
@Deprecated
annotation, let me know.
t
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.
l
What's your use case?
m
What you’re looking for is Macro support, which Kotlin doesn’t have.
r
@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.
l
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
r
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).
l
Then use the "Replace In Path" feature in the IDE (ctrl/cmd + shift + F).
r
What do you mean? How can you replace a function (which has a parameter) with its result like that?
l
you replace
f(10)
by the actual known value
r
And do that for all values? 😝 Deprecating
f
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.