How should an inline function on a Kotlin class be imported - kotlin

There is a kotlin file in a project dependency jar (called, for example, KotlinClass) with an inline function
package io.pack
inline fun <T> every(){
///does stuff
}
If I import it into a Java class as a static:
import static io.pack.KotlinClass.every;
The import is recognised.
If I import it into a Kotlin class:
import io.pack.every
or (this ought not to work anyway, but tried for completeness) as
import io.pack.KotlinClass.every
It is not recognised.
(Note: If I create my own Kotlin file with an inline function, then that can be imported into a Kotlin class with no problem. The problem is when importing from a specific project dependency.)
What might be stopping the import of this function into a kotlin class?

It works for me. I create a Kotlin module with a file called TopLevelStandalone.kt:
package io.pack
inline fun every(){
print("Inline")
}
fun normalFun() {
print("Normal")
}
The jar this project builds into contains the class file, and a META-INF/top-level-standalone.kotlin_module containing:
io.packTopLevelStandaloneKt
I then create another Kotline module, manually adding the jar to its dependencies. I'm now able to call every or normalFun:
import io.pack.every
import io.pack.normalFun
fun main(args: Array<String>) {
every()
normalFun()
}

Related

Kotlin class won't let me extend abstract class from different file

I am working on an object-oriented project, and I want to improve my understanding of the OOP part of Kotlin. I have the following abstract class:
package Objecttest
abstract class Abstractclasstest {
abstract fun testString(s: String): String
}
Now I want to extend it in a new class in a different file like this:
package Objecttest
public class Newclasstest : Abstractclasstest() {
override fun testString(s: String): String {
return s
}
}
but when I try compiling Newclasstest.kt, I am met with the following error message: "error: unresolved reference: Abstractclasstest".
Folder structure:
Objecttest/
├── Abstractclasstest.kt
├── Newclasstest.kt
Why is this and how can I work around it? The most important bit is why, because I want to avoid the same mistake in the future.
It seems to me that you need to compile Abstractclasstest.kt first using kotlinc Abstractclasstest.kt and only then compile Newclasstest.kt as follows: kotlinc -cp . Newclasstest.kt. This will search for class files in the same path as Newclasstest.kt on which it should already find the one corresponding to Abstractclasstest.kt. Or you can just compile the 2 files at the same time using kotlinc *.kt.

Kotlin - How to be able to run and test function?

I am creating demo code in Kotlin. I am trying so students should be able:
run function itself
run test
For example:
If function is created in .kt file, outside of class:
fun main(){
print("Hello world!")
}
it can be run
but I could not find the way to call it from the test
If the function is inside the class:
class Hello {
fun main(){
print("Hello world!")
}
}
the function can be called from the test
but can not be run - the green "Run" button is not visible.
Question: How to make such a function can be run manually and by test at the same time?
I'll assume you are writing your tests in Java, because if it's in Kotlin, calling main is trivial: main(), provided that you have imported the package/in the same package.
Kotlin global functions are compiled into static methods of a class with a name similar to the Kotlin file in which the function is declared, suffixed with Kt For example, if the file is called "app.kt", the class name would be AppKt. So if you declared main in app.kt, you would call:
AppKt.main();
in Java
You can change this name by annotating the Kotlin file with #JvmName:
#file:JvmName("MyOwnName")
Then you can call:
MyOwnName.main();
in Java.
See more documentation here

How to call a kotlin object method(in aar) from java?

An aar file has a kotlin class like
package com.material.utils;
object Utils {
fun init() {
System.loadLibrary("utils-jni")
}
}
How to call this Utils.init() from a java project that uses this kotlin aar?
You call it like this
Utils.INSTANCE.init();

Error:(1, 41) Kotlin: Symbol is declared in module 'jdk.internal.opt' which does not export package 'jdk.i

I have a beginner problem.
Just installed IntellijIDEA and JDK (Java Development Kit) and can't build my project.
Code:
import jdk.internal.joptsimple.internal.Strings
fun main(args: Array<Strings>){println("Hello")}
Error:
Error:(1, 41) Kotlin: Symbol is declared in module 'jdk.internal.opt' which does not export package 'jdk.internal.joptsimple.internal'
The proper main function with arguments looks like this:
fun main(args: Array<String>) {
}
Note that args is an array of Kotlin's String not jdk.internal.joptsimple.internal.Strings. So just fix your method's signature and remove the import statement.

Kotlin: Kotlin-script (.kts) cannot use regular code?

In my library's codebase, I have this package function: fun sayHello() = println("Hello there!")
The function is defined in the package org.jire.pomade
I would like to use this function in a .kts file like so: sayHello()
Unfortunately I can't seem to get code apart from Kotlin's own stdlib to work in Kotlin-script files.
The entirety of my script:
import org.jire.pomade.sayHello
sayHello()
The result of running the script:
pomade.kts:1:12: error: unresolved reference: jire
import org.jire.pomade.sayHello
^
pomade.kts:3:1: error: unresolved reference: sayHello
sayHello()
^
Anybody know why this is happening? Thanks.
This is a bug in the Kotlin plugin: https://youtrack.jetbrains.com/issue/KT-11618
I suggest to use holgerbrandl/kscript to manage dependencies of your script.
There is experimental support for maven imports in Kotlin scripts since 1.3.
Take a look at https://blog.jetbrains.com/kotlin/2018/09/kotlin-1-3-rc-is-here-migrate-your-coroutines/#scripting:
#file:Repository("https://jcenter.bintray.com")
#file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.11")
import kotlinx.html.*
import kotlinx.html.stream.*
print(createHTML().html {
body {
h1 { +"Hello, World!" }
}
})
And here is the KEEP: https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md.
Alternative is using the Kotlin REPL instead.