I get unresolved reference on either
Random
or .nextInt()
when using any of those imports import java.util.* import kotlin.random.*
on Android Studio with Kotlin.
I've used both import java.util.* and
import kotlin.random.* and the unresolved reference error appears on either the Class or the method. How do I import Kotlin.random properly?
kotlin.random and its associated classes are actually included in the Kotlin standard library, so your existing reference should be working:
import kotlin.random.Random;
fun main() {
println(Random.nextInt())
}
What it sounds like is that the appropriate references/libraries for Kotlin and/or Java aren't being properly loaded for your project. Depending on your environment (e.g. Gradle, Maven, etc.), you may want to force a compile or install step to ensure the dependencies are available.
Related
I'm new to Kotlin coming from the Python world, and wanted to get into Web Development with Kotlin with Ktor.
Now I've started the tutorial https://ktor.io/docs/creating-interactive-website.html#running-our-application-for-the-first-time and already I don't understand quite a few things.
Looking at the code for Application.kt, which is
package com.jetbrains.handson.website
import freemarker.cache.*
import freemarker.core.HTMLOutputFormat
import io.ktor.application.*
import io.ktor.freemarker.*
import io.ktor.html.respondHtml
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.*
import io.ktor.request.receiveParameters
import io.ktor.response.*
import io.ktor.routing.*
import kotlinx.html.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module() {
routing {
static("/static") {
resources("files")
}
}
}
I don't understand what the syntax means. I'm assuming module is an extension function for the class Application? But what does routing and static mean. Can someone explain the concepts happening there, or maybe just name them so I can google them.
Thanks in advance :-)
Edit: For clarification. I know where to look for when it comes to routing concepts and such for ktor. What I am asking is what routing is. Is it a function? But if so what parameters does it take, it does not look like a normal function definition and it is declared inside another function. When I look at the types it seems it takes a lambda function with type Routing.() -> Unit. What does Routing.() mean...
That's what I don't understand.
The routing is an extension function of the Application that installs the Routing plugin and configures it using a provided lambda. The Route.() -> Unit is a function type with the Route receiver (this has type Route inside this function) that has no parameters and that returns Unit (nothing). For more information please read the official Kotlin documentation. So it's the build pattern in action, that allows writing DSLs like Ktor's routing.
I want to make my custom inline function in .kt file using checkRadix function already implemented by Kotlin.
But I cannot import it. How can I import and use it?
I tried
import kotlin.jvm.JvmMultifileClass.*
kotlin.jvm.JvmMultifileClass.checkRadix(radix)
But I can't compile and there is no recommended resolution by IDE.
That function is marked as internal, which means it's only available within that module — i.e. within the Kotlin stdlib, not to your code.
I don't know why it's marked like that; maybe JetBrains consider it an implementation detail. But they clearly don't want it being used by any other code.
(Of course, it wouldn't be hard to reimplement yourself.)
I have been dealing with kotlin multiplatform alot recently, and I totaly understand the nature of the development. Initially, I had my own expected Math class (in a common module) and I had actual classes in the JS and JVM environment.
Since I like to read the documentations, I found that the Math liblary has been added to the standard liblary since kotlin 1.2. this trouble me as I am using kotlin 1.2.51 and I get an error trying to access the class from kotlin.Math in my common module and any of my platform specific module.
What am I not geting? How do I get access to the kotlin.Math class in my common module?
The Math-class is deprecated and the deprecated-message contains:
Use top-level functions from kotlin.math package instead.
(see also https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-math/index.html)
So somehow the answer of #mTak is right, even though it was not mentioned, that you should use the kotlin.math.*-import instead of your Math-class.
Alternatively, you can also import kotlin.math.max, etc. depending on which function you actually require.
The more I think of it: I don't know whether there ever was a Math-class in the jvm-variant of Kotlin (couldn't find anything regarding it) and so in a multiplatform project the Math-class-access probably should always have failed.
Import it like this: import kotlin.math.*
In the Kotlin standard library math functions are provided as top-level functions in the kotlin.math package.
Therefore you need to import that package and then you'll be able to use functions from it, like sin, sqrt and so on.
import kotlin.math.*
val sqrt2 = sqrt(2.0)
You can also import functions one by one, e.g. import kotlin.math.sqrt or even call them fully qualified val result = kotlin.math.sqrt(2.0)
After a while (I even feel stupid). I found that the kotlin.math library in kotlin common modules was already added. The only difference was, it had no the 'Math.' predecessor as I am normally used to.
So, Math.round(x: Float) was just round(x: Float)
Math.sin(x: Float) was just sin(x: Float)
This is more an question about intellij (I think).
Recently updated my intellij and some of my prferences have been changed
when a class is put into a package it can no longer access any other classes, When starting a new project I have clicked auto import as always but it will not allow me to extend a class from an abstract class without both classes being at the same level.
Also when starting a new project a java module is no longer generated.
problem
Thank you for your help in advance (If you have not already guested I am farily new to programming)
You cannot import a Class from the default package.
Move the Player class into a package and everything will work fine.
See: What's the syntax to import a class in a default package in Java?
In Java the java.lang package is imported by default.
In kotlin a number of functions and classes are available without being imported, like println and kotlins Array, Int, etc types.
What else is imported by default and where is it defined?
Kotlin stdlib has kotlin root package and its subpackages (see the full list with the content).
It seems not to be documented anywhere which of them are imported by default, but a peek into Kotlin Github sources suggests that these packages are imported for JVM target platform:
java.lang.*
kotlin.*
kotlin.annotation.*
kotlin.jvm.*
kotlin.collections.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
kotlin.io.*
kotlin.coroutines.* (to be added in Kotlin 1.1, not present in 1.0.4)
I've manually tested them, and the list above is true for Kotlin 1.0.4. And these stdlib packages are not imported by default:
kotlin.comparisons.*
kotlin.concurrent.*
kotlin.properties.*
kotlin.reflect.*
kotlin.reflect.jvm.*
kotlin.system.*
As #Magnus noted, the default imports for JS platform are different.
The official documentation for the list of Kotlin's default imports (which is likely to be change with new versions of the language) is here:
https://kotlinlang.org/docs/reference/packages.html#default-imports
As of 2018-02-11 it includes the following:
kotlin.*
kotlin.annotation.*
kotlin.collections.*
kotlin.comparisons.* (since 1.1)
kotlin.io.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
Additional packages are imported depending on the target platform:
JVM:
java.lang.*
kotlin.jvm.*
JS:
kotlin.js.*