Questions tagged [kotlin]

Kotlin is a cross-platform, statically typed, general-purpose high-level programming language with type inference. This tag is often used alongside additional tags for the different targets (JVM, JavaScript, native, etc.) and libraries/frameworks (Android, Spring, etc.) used by Kotlin developers, if the question relates specifically to those topics.

Kotlin is an open-source, statically typed programming language supported and developed by JetBrains. It supports JVM bytecode, JavaScript, and Native code as compilation targets, and it has been an officially supported first-class language on Android since Google I/O 2017. The goals with Kotlin are to make it concise, safe, versatile, and have it be seamlessly interoperable with existing Java libraries.

On June 9th 2022, The Kotlin team released version 1.7.0 (announcement / github release tag).

The current major version is 1.7.

How to ask

If you are using Kotlin and your question is related to it, then you should add this tag. You should explain what do you intend to achieve, how did you try to achieve it, what the experienced behavior is and how is that different from your expectations.

Kotlin Reference Documentation

Kotlin Books

Development tools

Useful links

91039 questions
22
votes
3 answers

Calling kotlin functions which are keywords in java from java?

Since new is not a keyword in kotlin, i can have the following function in kotlin. fun new(): String { return "just returns some string" } But i am unable to call this function from java since new is a keyword in java. I would like to know if…
solo_assassin
  • 483
  • 4
  • 20
22
votes
2 answers

Why can't I reference a nested object using `val` or `typealias` that refers to an object?

Consider the following code: object SomeObjectA { object SomeObjectB { val a = "test" } } val X = SomeObjectA typealias Y = SomeObjectA SomeObjectA.SomeObjectB // works X.SomeObjectB // error Y.SomeObjectB // error I can't use val…
Naetmul
  • 14,544
  • 8
  • 57
  • 81
22
votes
4 answers

Kotlin - println using string template to stderr

How do I send output of println() to System.err. I want to use string template. val i = 3 println("my number is $i") println() sends message to stdout and it looks like there is no option to send to stderr.
mjlee
  • 3,374
  • 4
  • 27
  • 22
22
votes
6 answers

compileReleaseKotlin fails with java.lang.ClassNotFoundException: com.sun.tools.javac.util.Context

I'm trying to build my Android Project (which contains a library module) via terminal using gradlew. From within Android Studio, it compiles and installs successfully but, when I try to run ./gradlew assembleDebug I get the following…
Humble Student
  • 3,755
  • 4
  • 20
  • 35
22
votes
4 answers

RxJava 2 overriding IO scheduler in unit test

I'm trying to test the following RxKotlin/RxJava 2 code: validate(data) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .flatMap { ... } I'm attempting to override the schedulers as follows: // Runs before each…
Alex
  • 739
  • 1
  • 6
  • 18
22
votes
7 answers

Count number of digits in Kotlin

I'm currently counting number of digits using simple string.length approach: val number = 829 val length = number.toString().length I wonder whether this is a good way or there is a more appropriate way to do that in Kotlin.
arslancharyev31
  • 1,801
  • 3
  • 21
  • 39
22
votes
1 answer

Kotlin Android Studio/IntelliJ "Can be joined with assignment" Inspection Warning

I'm new to Koltin and really loving it so far, but I've hit a snag. I'm sure I'm missing something extremely, extremely basic here, but nonetheless, I'm a loss, and I appreciate any help. I converted a simple java class to Kotlin using the Android…
madcow
  • 2,567
  • 14
  • 31
22
votes
1 answer

How to convert LocalDateTime object into ISO string including time zone?

I am trying to convert a date/time string back and forth into a LocalDateTime object. I am using ThreeTenBp as the date/time library. String -> LocalDateTime val actual = LocalDateTime.parse("2016-12-27T08:15:05.674+01:00", …
JJD
  • 50,076
  • 60
  • 203
  • 339
22
votes
3 answers

Kotlin - Override/Implement array-like accessor function

Is it possible to override or implement the [] accessors in Kotlin (using operator overloading or similar)? val testObject = MyCustumObject() println(testObject["hi"]) // i.e. implement this accessor. In Python this is possible by implementing…
Robin Nabel
  • 2,170
  • 1
  • 21
  • 26
22
votes
2 answers

How to create an immutable list in Kotlin that is also an immutable list in Java?

I have a Java/Kotlin interop problem. A Kotlin immutable list is compiled into a normal java.util.ArrayList that is mutable! Kotlin (library): class A { val items: List = ArrayList() } Java (consumer): A a = new A(); a.getItems().add(new…
WindRider
  • 11,958
  • 6
  • 50
  • 57
22
votes
2 answers

Gson Deserialization with Kotlin, Initializer block not called

My initializer block is working perfectly fine when I create my Object class ObjectToDeserialize(var someString: String = "") : Serializable { init{ someString += " initialized" } } this way: @Test fun…
Lukas Lechner
  • 7,881
  • 7
  • 40
  • 53
22
votes
1 answer

How to verify call on setter in kotlin using mockito?

interface LoginDisplay { var username: String var password: String } class LoginActivityLoginDisplay : LoginDisplay { override var username: String get() = usernameEditView.text.toString() set(value) { …
przebar
  • 531
  • 1
  • 5
  • 19
22
votes
1 answer

Reassigning Variables via Destructuring

I love Kotlin's destructuring features, they help me to declutter code and focus on the essential. I encountered a case for which I could not figure out the correct syntax, how can I reassign variables via destructing? var (start, end) =…
linqu
  • 11,320
  • 8
  • 55
  • 67
22
votes
3 answers

Configure IntelliJ auto-completion for gradle script in kotlin

I am trying out gradle-script-kotlin with simple hello-world application in IntelliJ. But IntelliJ auto-completion doesn't popup in build.gradle.kts…
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
22
votes
2 answers

How to create package-level functions?

I was reading the Kotlin Reference Guide and one part said: In Kotlin, unlike Java or C#, classes do not have static methods. In most cases, it’s recommended to simply use package-level functions instead. How does one create a package-level…
Berry
  • 2,143
  • 4
  • 23
  • 46