Escape reserved words in an import - kotlin

When using Java classes in Kotlin, sometimes they use identifiers that are reserved word in kotlin. The java interop documentation says you can use backtick (`) character but this doesn't work in imports. So how to escaped reserved words on import.
e.g. The tinkerpop library has a class called '__' but thats a Kotlin reserved so if I import it like below it gives an error.
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__
I can import it using the wild card like this:
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.*
but because I only use the one class then Intellij's code formatter replaces it with the previous version of the import that gives an error. (Which I guess is a bug in the intellij formatter as well)
For the answers that suggest using backtick ` works. It does not. This does not compile even on try.kotlinlang.org.
Assuming a class exists defined as
package blah
class `__` {
}
This is what happens when you import it with backticks.

Disclaimer: I am not a Kotlin user.
The Kotlin Grammar page ( https://kotlinlang.org/docs/reference/grammar.html#SimpleName ) states that the import keyword is followed by a SimpleName ("." SimpleName) where SimpleName is defined as either <java identifier> or "``" <java identifier> "``"
According to the above rules, each component that needs escaping should be surrounded in single backtick characters. As only the last name is reserved your code should look like this:
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.`__`
I tried using this in an online Kotlin tool ( http://try.kotlinlang.org/ ) and the syntax was accepted.
Update
I created a 2-file program in try.kotlinlang.org and I can reproduce the problem, and fix it.
So the issue isn't just that keywords and other reserved words need to be escaped with backticks (though in this case, the name __ (two underscores) is not a keyword in Kotlin, but that Kotlin does not seem to allow reserved names to even exist in-scope!
Here's my test-case:
File1.kt
package blah
class `__` {
fun foo(): Int {
return 5
}
}
File2.kt
import blah.__
fun test2() {
val us = __()
us.foo
}
This gives me this compile-time error:
File2.kt - Error:(1,) Names _, , _, ..., are reserved in Kotlin
If I change File2.kt to this, then it works fine:
import blah.__ as underscore
fun test2() {
val us = underscore()
us.foo
}
Using backticks doesn't seem to help at all, but it does give me unexpected errors:
import blah.`__` as `__`
fun test2() {
val us = `__`() <-- error is here
us.foo()
}
File2.kt - Error: (4, 13) Unresolved reference: __
Variations of the names with backticks and unescaped double-underscores yields no effect.
So it seems the solution is to alias the name to something else during import.

One method I've found that works is to import it with an alias which is ok but it does change the name used in code.
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ as underscore

As specified in the docs, your import must look like:
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.`__`
Then to use the underscore symbol in the code you'll have to escape it every time, so an alias is the best solution:
`__`.foo()
Docs: https://kotlinlang.org/docs/reference/java-interop.html#escaping-for-java-identifiers-that-are-keywords-in-kotlin

Related

Can't compile Kotlin Jackson Extensions readValue(File)

I am trying to use Kotlin Jackson extensions to do JSON conversions in my code. But for some reason, I am getting a syntax error when trying to use the readValue(File) function.
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3'
---
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
...
private val objectMapper = jacksonObjectMapper()
...
val factionList: List<Faction> = objectMapper.readValue<List<Faction>>(
File(javaClass.classLoader.getResource("data/factions.json").file))
The error I get from the compiler is:
None of the following functions can be called with the arguments supplied.
readValue(JsonParser!, ResolvedType!)
...
[it lists all the valid function signatures ...]
However, none of the extension functions seem to be showing up in that list. If I click on the function and hit Cmd-B in IntelliJ, I am seeing the readValue(File) method in the extensions code.
I am confused why the function is not being found by the compiler.
You're most likely missing the following import:
import com.fasterxml.jackson.module.kotlin.readValue
Forgot this a few times myself.

How to write `$("user")` in kotlin with flink

from flink 1.14, it suggest using $() for retrieving elements like below:
Table table = tableEnv.fromDataStream(ds, $("user"), $("product"), $("amount"));
so is there a way to convert it to kotlin, for it causing compiler error in kotlin:
var table = tableEnv.fromDataStream(ds, $("user"), $("product"), $("amount"));
I am not familiar with Apache Flink, but you're not allowed to use the dollar sign on it's own.
For function names with special characters, e.g. whitespace, you have to put the function name into backticks (`), e.g.:
fun `white space`() = Unit
fun `$`() = Unit
fun main() {
`white space`()
`$`()
}
Thus, your code example above works on my machine with the following adjustments.
import org.apache.flink.table.api.Expressions.*
...
val table = tableEnv.fromDataStream(ds, `$`("user"), `$`("product"), `$`("amount"))
This problem will be fixed in one of the next Flink releases:
https://issues.apache.org/jira/browse/FLINK-26278

function min in Kotlin

i use the Mathematical function "min" in my Kotlin code to declare a variable "toRemove"
val toRemove = min(preferredQuantity - taken, stock.quantity)
error message : Kotlin unresolved reference
1/ may i know how could i solve it?
2/ the function is within kotlin.math, why I cannot use it directly?
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/
Thanks!
For completeness' sake: as pointed out by #Tenfour04 in the comments, make sure that kotlin.math.min is imported by checking that one of
import kotlin.math.min
import kotlin.math.*
is on top of the file. The kotlin.math.* variant will import everything from the kotlin.math package. See the Kotlin documentation page on imports.

Is there a way to suppress Kotlin's default import?

Is there a way to suppress (e.g., via a commnad line flag passed to the compiler) Kotlin's default import of multiple packages? or - alternatively - to be selective about it?
This is easy, just use your alternative to replace it by using as, and the default import will be replaced by your one.
Here's a simple example, if you want to use java.lang.String instead of kotlin.String, although it's not recommended, this is just an example.
import java.lang.String as String
// here, String is not `kotlin.String`.
private fun main(vararg args: String) {
}
BTW there's a trick about refactoring, like if you want to replace all Any used in a file with java.lang.Object, put this after the package declaration:
import java.lang.Object as Any
And the implicit import to Any is suppressed and superseded by Object.

Extension function collision [duplicate]

If I have a jar, on the classpath, where I've created an extension function on say the String class for argument's sake and I have another jar with the same extension function on String, how will Kotlin resolve the two?
I presume if both functions are defined in the same packages then there will be a clash?
But if different packages, how I can distinguish the two extensions?
Indeed, if they're in the same package, it won't compile. For the other scenario, let's say you have two files with two different packages, containing extension functions with the same signature:
First file:
package ext1
fun Int.print() = print(this)
Second file:
package ext2
fun Int.print() = print(this * 2)
And this file where you're trying to use it:
package main
fun main(args: Array<String>) {
42.print()
}
IntelliJ will actually give you an import dialog where you can choose which one you want to use:
You can import one of them like this:
import ext1.print
And if you need to use the other one as well, you can rename it with the as keyword. This keyword works for imports in general, classes with the same name, etc.
import ext2.print as print2
So this program compiles and prints 4284:
package main
import ext1.print
import ext2.print as print2
fun main(args: Array<String>) {
42.print()
42.print2()
}
As a quick note, the one you import with the as keyword will be slightly harder to use, as autocomplete doesn't seem to pick it up well, selecting the second option here just completes the call to 42.print().
So since extension function in kotlin is just static function, other functions will be distinguish by import.
Also you can make alias for one of extension functions for more readability:
import by.bkug.extensions.helpers.extension
import by.bkug.extensions.extension as extension1
fun test() {
myType().extension() // by.bkug.extensions.helpers.extension
myType().extention1() // by.bkug.extensions.extension
}