Import package


#1

Hello, just started to learn kotlin(and i am new to java so…) and I have a question.
I wrote some function, for example :

fun discriminant(a: Double, b: Double, c: Double) = sqr(b) - 4 * a * c

and I need to use it in another file, which is in another folder. Compiled the code with this function with

kotlinc Simple.kt -include-runtime -d hello.jar

so in another file i wrote

import lesson.task.discriminant

and have error like this

IfElse.kt:4:8: error: unresolved reference: lesson
import lesson.task.discriminant
^
IfElse.kt:20:13: error: unresolved reference: discriminant
val d = discriminant(a, b, c) // 2

Think i need to change smt with link options , i tried to add

-cp /path/to/class

What should i do to correctly link this function to my new file?

Thanks


#2

Have you declared the package in the file where your discriminant is? Kotlin packages are not inferred from the package structure (although they typically match).


#3

Yes i did, the whole first file is

package lesson.task
import java.lang.Math.*
fun discriminant(a: Double, b: Double, c: Double) = sqr(b) - 4 * a * c


#4

Anyway i cant understand how kotlin will find package if i write

“import lesson.task.discriminant”

Think i should write the folder where to find this package , or path to compiled .jar file.


#5

Basically, you need to put the class tree you want to access to your class path and remember that in java and kotlin package equals directory. If you want to access lesson.task.discriminant function, then you you must have a directory or jar on your classpath that has directory lesson/task inside it and some compiled kotlin class with top level function inside this directory.

Probably you want not to link the package, but to compile two classes together, remembering that package == path.

Anyway, I believe that build tools are one of the best things in java ecosystem, so instead of reinventing the wheel, you should look into that.