Kotlin unresolved reference to local package

Viewed 952

File ./Main.kt:

import numbers

fun main() {
    var value = numbers.sum(7, 4)
}

File ./numbers/Operations.kt:

package numbers

fun sum(val1: Int, val2: Int): Int {
    return val1 + val2
}

Running the command kotlinc Main.kt outputs:

Main.kt:1:8: error: unresolved reference: numbers
import numbers

Main.kt:4:17: error: unresolved reference: numbers
var value = numbers.sum(2, 3)

If I try to add import java (I am new in Kotlin ans I saw someone imported this. I thought it is a part of a standard library) then output also contains:

Main.kt:1:8: error: packages cannot be imported
import java

What I did wrong?

SOLUTION (Thanks to @gidds):

Was need to pass each file to the complier, not like with Java compiler (javac)

kotlinc Main.kt numbers/Operations.kt
1 Answers

numbers is a package.  And, as the error says, packages cannot be imported.  Only classes, functions, or properties.

If you import numbers.sum, you should find you can call it with e.g. sum(7, 4).

Or you could call its fully-qualified name directly, without the import: numbers.sum(7, 4).

It's the same with java, which is also a package.  You can import java.util.concurrent.ConcurrentHashMap (a class), but not java, java.util, nor java.util.concurrent (which are all packages).  (Java has the same restriction.)

Related