Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call kotlin's js function with an interpolated string template?

Right now, IntellJ is showing a red squiggly line saying: Argument must be a string constant

private fun fromEnv(name: String) {
    return js("process.env[${name}]") as Unit
}

I've searched but I have not found any similar question.


Solved by @alexey-romanov

It's just as simple as:

private fun fromEnv(name: String) {
    return js("process.env[${name}]") as Unit
}

which compiles to:

function fromEnv(name) {
  var tmp$;
  return typeof (tmp$ = process.env[name]) === 'string' ? tmp$ : throwCCE();
}
like image 982
Richard Domingo Avatar asked Dec 05 '25 00:12

Richard Domingo


1 Answers

No, it isn't. But you can just use name in the code argument to js:

private fun fromEnv(name: String) {
     js("process.env[name]")
}

This example is pretty much the same as the use of the variable o in the Inline Javascript section of documentation:

fun jsTypeOf(o: Any): String {
    return js("typeof o")
}
like image 163
Alexey Romanov Avatar answered Dec 06 '25 15:12

Alexey Romanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!