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
130
votes
4 answers

Mark unused parameters in Kotlin

I am defining some functions to be used as callbacks and not all of them use all their parameters. How can I mark unused parameters so that the compiler won't give me warnings about them?
TheTeaMan
  • 2,373
  • 3
  • 15
  • 13
128
votes
2 answers

Extend and implement at the same time in Kotlin

In Java, you can do such thing as: class MyClass extends SuperClass implements MyInterface, ... Is it possible to do the same thing in Kotlin? Assuming SuperClass is abstract and does not implement MyInterface
chntgomez
  • 2,058
  • 3
  • 19
  • 31
128
votes
9 answers

How to return multiple values from a function in Kotlin like we do in Swift?

How to return 3 separate data values of the same type (Int) from a function in Kotlin? I'm attempting to return the time of day, I need to return the Hour, Minute and Second as separate integers, but all in one go from the same function, is this…
AwaisMajeed
  • 2,254
  • 2
  • 10
  • 25
126
votes
18 answers

onBackPressed() is deprecated. What is the alternative?

I have upgraded targetSdkVersion and compileSdkVersion to 33. I am now getting a warning telling me that onBackPressed is deprecated. I see suggestions to use android.window.OnBackInvokedCallback or androidx.activity.OnBackPressedCallback to handle…
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
126
votes
6 answers

Spring Boot 2.5.0 generates plain.jar file. Can I remove it?

After the Spring Boot 2.5.0 update, it generates the myprogram-0.0.1-plain.jar file alongside the usual myprogram-0.0.1.jar. Can I disallow gradle to generate the *.plain.jar file? I use Gradle 7.0.2. What I get: build/ libs/ …
Tien Do Nam
  • 3,630
  • 3
  • 15
  • 30
124
votes
5 answers

In Kotlin can I create a range that counts backwards?

I looked at the documentation for the Ranges and I see no mention of backwards ranges. Is it possible to do something like: for (n in 100..1) { println(n) } And get results: 100 99 98 ...
jjnguy
  • 136,852
  • 53
  • 295
  • 323
124
votes
4 answers

Why can't 'kotlin.Result' be used as a return type?

I've created a method, and the return is Result in a class of MyClass, but the error message is: 'kotlin.Result' cannot be used as a return type I've also looked into the Result source code for some hints; why is this so? Test code (using v.…
ersin-ertan
  • 2,283
  • 2
  • 18
  • 27
124
votes
16 answers

How to check permission in fragment

I want to check a permission inside a fragment. my code: // Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) !=…
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
124
votes
7 answers

How to initialize List in Kotlin?

I see Kotlin has a List collection and I was wondering about different ways to initialize one. In Java, I could write: List geeks = Arrays.asList("Fowler", "Beck", "Evans"); How can I achieve the same in Kotlin?
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
124
votes
1 answer

"Prefer to run the dagger processor over that class instead" in Kotlin

I'm getting this note in the build whenever I do an inject into a kotlin class (btw, I have a mixed android project with both kotlin and java). For example, after this gradle task: compileStagingDebugJavaWithJavac (StagingDebug is my build variant),…
Fábio Carballo
  • 3,245
  • 5
  • 26
  • 36
124
votes
1 answer

Generic extending class AND implements interface in Kotlin

Say I want a type variable, T, that extends a certain class and implements an interface. Something like: class Foo { ... } What is the syntax for this in Kotlin?
frenchdonuts
  • 1,342
  • 2
  • 8
  • 7
123
votes
10 answers

Notify Observer when item is added to List of LiveData

I need to get an Observer event when the item is added to the List of LiveData. But as far as I understand the event receives only when I replace the old list with a new one. For example when I do the next: list.value =…
Ruslan Leshchenko
  • 4,043
  • 4
  • 24
  • 36
123
votes
6 answers

How to add an item to an ArrayList in Kotlin?

How to add an item to an ArrayList in Kotlin?
Ramesh
  • 1,772
  • 4
  • 12
  • 15
122
votes
7 answers

Kotlin - Idiomatic way to check array contains value

What's an idiomatic way to check if an array of strings contains a value in Kotlin? Just like ruby's #include?. I thought about: array.filter { it == "value" }.any() Is there a better way?
jturolla
  • 6,596
  • 7
  • 26
  • 41
121
votes
21 answers

android.view.View.systemUiVisibility deprecated. What is the replacement?

I have updated the project target API version to 30, and now I see that the systemUiVisibility property is deprecated. The following kotlin code is the one I'm using which is actually equivalent to setSystemUiVisibility method in…
Doctiger
  • 2,318
  • 2
  • 15
  • 28