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

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.

Related

Remove import when using Kotlin's #Deprecated "ReplaceWith" annotation

I have a custom function MutableStateFlow.update(), which i want to deprecate in favor of the official "kotlinx.coroutines.flow.update" version.
#Deprecated(
message = "This is not thread safe. Use the official version from kotlinx.coroutines.flow.update",
ReplaceWith("update(transformation)", "kotlinx.coroutines.flow.update"),
DeprecationLevel.ERROR
)
public inline fun <T> MutableStateFlow<T>.update(transformation: (previousValue: T) -> T) {
value = transformation(value)
}
This works ok-ish:
The main issue is that the signature of the old & the new fun are the same, so the IDE gets confused which to choose, since both are imported.
I therefore need to remove the import for my old version when the ReplaceWith is called. Is this possible? (Is there a better alternative?)
In case it matters: We're working mainly with Android Studio
If this is all in one open project, you could Refactor->Rename your update function (Shift + F6) to some unique name. Then there won't be the import conflict.

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.

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
}

Escape reserved words in an import

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

Kotlin: Why can't I use one of my functions?

I am trying to use one of my defined functions that accepts a string yet the software won't compile.
fun passes(address: String) = Collections.frequency(addresses, address) <= CONNECTIONS_PER_IP
fun passes(remoteAddress: InetSocketAddress) = passes(remoteAddress.hostName)
I can't even call the string function using a custom string, for example passes("127.0.0.1").
None of the following functions can eb called with the arguments supplied.
passes(String) defined in abendigo.Firewall
passes(InetSocketAddress) defined in abendigo.Firewall
I presume you're using java.lang.String instead of kotlin.String in the Kotlin source code. Please use only kotlin.String instead, this is the type that string literals in Kotlin have (but in the bytecode it's still transformed to java.lang.String).
The issue was an import of java.lang.String. For some reason IntelliJ imported it.