https://kotlinlang.org logo
Join Slack
Powered by
# forum
  • r

    rss

    08/11/2025, 2:19 PM
    Working with "Any" Variables Help Hi friends, I have a query. I need the returned variable to print details and price term.details term.price but it won’t let me. It prints Product(details=Mouse Usb xxxx, price=3.25) another query. Is it okay to work with Any in this type of function? Thanks package com.example.sys fun main(){ var term:Any=ret() if (term is String) { println("printer:${term}") } else { println("printer:${term}") } } fun ret():Any { data class Product(val details:...
  • r

    rss

    08/12/2025, 3:59 PM
    Let's have an intersection sugar interface Developer { fun develop() } interface BeerExpert { fun drink() } fun doAll(u: T) where T : Developer, T : BeerExpert { u.develop() u.drink() } // I wish that could be written as fun doAll(u: Developer & BeerExpert) { u.develop() u.drink() } Inspiration: Handbook - Unions and Intersection Types 1 post - 1 participant <a...
  • r

    rss

    08/13/2025, 4:19 PM
    listOf get index numbers I am following this tutorial

    here▾

    . class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() val people = listOf("John", "Jack", "DJ", "Luke", "Matt", "Andrew", "Dave", "Steven", "Billy", "Zane", "Bobby", "Ryan", "Martin", "Werner", "Peter") setContent { RSSReaderTheme...
  • r

    rss

    08/13/2025, 10:19 PM
    A new feature for default arguments Hello! Image a function that has a default argument. Is there a way to call this function with a certain argument if a condition is met, else use the default argument? Here’s some code: fun foo(arg: String = "Default value”) { … } fun main() { foo( arg = if (...) "My value" else default() // I'm inventing here a default function (a possible new feature, which of course can also be made in other ways) which calls the function with its default argument! ) } Here, the default argument is...
  • r

    rss

    08/17/2025, 11:19 AM
    Why does my Kotlin Android app crash when using coroutines in ViewModel? Hello I am developing an Android app in Kotlin and recently added coroutines to handle background tasks in my ViewModel. The issue is that when the screen rotates or the activity is recreated; the app sometimes crashes with JobCancellationException / other lifecycle-related errors. I expected the ViewModel scope to manage this automatically but it seems like my coroutines are still being cancelled unexpectedly. I tried using viewModelScope.launch as recommended & also tested with...
  • r

    rss

    08/21/2025, 7:59 PM
    New(?) idea for collection literals Typing this out from my phone so I haven’t tested the idea thoroughly but thought the idea maybe spark a good discussion. Collection literals will only be available when there is a “CollectionBuilder” in context. For example: fun interface ListBuilder { operator fun buildList(varargs elements: I): T } This would allow code looking something like this: val list = with(ArrayListBuilder) { [1, 2, 3] } Of course, Kotlin let’s us bring things into context in a lot of ways...
  • r

    rss

    08/28/2025, 3:09 PM
    Deconstruction with * operator, similar to vararg I often want to call functions with an object, but I need to specifiy the parameters explicitly: class Circle(val centerX: Int, val centerY: Int, val radius: Float) val point = IntOffset(100,200) val circle = Circle(point.width, point.height, 70f) but I’d like to avoid writing .width .height all over my code. I suggest to do this: val circle = Circle(*point, 70f) This is very similar to how * operator works for the vararg, and it seems natural to me. What do you think? 1...
  • r

    rss

    08/29/2025, 9:49 AM
    Support for "satisfies" like in TypeScript I want to write a function, that accepts any argument that just fullfills or satisfies an interface. E.g. My example interface Inf1 has an open() and a close() method. fun myFun(arg: satisfies Inf1) { arg.open(); … arg.close(); } myFun(anyObj); 1 post - 1 participant Read full topic
  • r

    rss

    08/31/2025, 9:09 AM
    Time out for reading from a stream I use InputStream.read() to read from a pipe after creating a sub-process. I had a problem that the process got stuck, so I wrote a quite complicated code with coroutines, sleep, available() and other tests to make sure I never get stuck and that join() will always return immediately. When I’d written similar code in C It was very easy - I didn’t even need to create a thread, since I could read directly from the output with a timeout. However, the process.outputStream and errorStream don’t...
  • r

    rss

    09/01/2025, 11:29 PM
    Error when compiling with K2JVMCompiler dynamically in Java Hi, I am new to Kotlin. We are planning on using kotlin to run pluggable scripts, so I am trying to compile kotlin scripts in java and add to the available classes. My java code puts the script text to a file and runs K2JVMCompiler. For testing I am trying to compile this script: class HelloWorld { fun sayHello(inp : String) : String { return "Hello from dynamic Kotlin!" + inp; } } in java I run this: `File kotlinSourceFile = new File(className + “.kt”); kotlinSourceFile.delete();...
  • r

    rss

    09/03/2025, 4:19 AM
    Intellij plugin for Kotlin script definitions I’m trying to make a plugin to add a custom script definition to IDEA. I’ve managed to make a script definition show up without any plugins if I have the project that contains the script definition loaded, but I’d like a more permanent solution. Right now I have the script definition package dev.fishies.cohoplugin import kotlin.script.experimental.annotations.KotlinScript import kotlin.script.experimental.api.ScriptAcceptedLocation import...
  • r

    rss

    09/04/2025, 5:59 PM
    Lsp + mcp Has anyone used the Kotlin LSP with MCP? 1 post - 1 participant Read full topic
  • r

    rss

    09/10/2025, 5:09 PM
    BottomModalLayout OnDismiss is closing on clicking outside the scrim val scope = rememberCoroutineScope() val sheetState = rememberModalBottomSheetState( skipPartiallyExpanded = true, confirmValueChange = { it == SheetValue.Expanded } ) val is_open = remember { mutableStateOf(false) } if(!is_open.value) { ModalBottomSheet( onDismissRequest = { }, dragHandle = null, sheetState = sheetState, modifier = Modifier.wrapContentSize(),...
  • r

    rss

    09/15/2025, 1:29 PM
    Black screen issue for the iOS platform during screen switch Hi, KMP community, during the development of the KMP + Compose UI application, I encountered an iOS-only UI issue. The problem looks like, when using the application in some random moment of the time, screens stay black, and when I click to another tab in half a second, I see the past screen and then the black screen, it’s also strange moment because during the click to a new tab I should see on the second current screen not a past screen. Also, one of my screens has a player with auto start....
  • r

    rss

    09/15/2025, 3:09 PM
    Throws annotations for Objective-C interop Hi, I have a quick question about exception handling when calling a Kotlin function from Swift/Objective-C code. Do I need to annotate all functions, including nested and helper functions, or is it enough to annotate only the public (API) function that is called from Swift? Best regards, Sven 1 post - 1 participant Read full topic
  • r

    rss

    09/19/2025, 2:39 AM
    Java 25 compatibility Any plans to support Java 25 now that gradle has released 9.1.0 with full support? 1 post - 1 participant Read full topic
  • r

    rss

    09/19/2025, 8:19 AM
    Is the new WebElementView meant for replacing usage of native web views? I see that Compose Multiplatform 1.9 has just released with the introduction of the new WebElementView New API for embedding HTML content I’m making a Compose Multiplatform (targeted to iOS and Android) that’s just a wrapper to one of our existing SPA web apps. I’m using WKWebView on iOS and WebView on Android, giving them both a URL...
  • r

    rss

    09/23/2025, 10:29 PM
    Why is method with vararg param selected when passing named arguments? Consider this code: class Log { private fun print(input: String, params: String) { println("${input.padEnd(15)}-> $params") } fun debug(input: String, vararg data: Any) { print(input, "vararg data: Any") } fun debug(input: String, data: Any) { print(input, "data: Any") } } fun main() { val log = Log() log.debug("data = [data]", data = arrayOf("data")) log.debug("[data]", arrayOf("data")) } The output is: data = [data] -> vararg...
  • r

    rss

    09/27/2025, 1:49 PM
    Access protected member of another instance of a superclass I have the class O that defines the protected field code. I have the class BO that extends O. In a method of BO that takes a BO as a parameter, I can access this.code and other.code. But in a method of BO that takes an O as a parameter, I can access this.code but not other.code. Why ? 1 post - 1 participant Read full topic
  • r

    rss

    09/28/2025, 2:29 PM
    Value class box-impl override When int is converted into Integer (autoboxing), compiler calls public static Integer valueOf(int i); When underlying field is converted into an instance of the value class, the compiler calls ValueClass.box-impl method which just wraps constructor. I would like to implement cache in similar way as Integer does, but I cannot override the method. I tried sth with e.g. @JvmName @JvmStatic, but result method returns the underlying field instead of instence of the class and then it is not called...
  • r

    rss

    09/28/2025, 6:49 PM
    Difference between function type with receiver and extension function Consider the following functions: fun Int.ext() { // TODO } fun doSomething(f: Int.() -> Unit) { // TODO } The functional parameter f has the same type as the extension function ext. So, I can do this: doSomething(Int::ext) The question is… In the body of doSomething function, I can do this: fun doSomething(f: Int.() -> Unit) { 42.f() // OK f(42) // OK } But when I call the ext function, this happens: 42.ext() // OK ext(42) // compiler error Why does that...
  • r

    rss

    10/01/2025, 9:49 AM
    Unbelievable error message after compile Hello everybody¡¡ I’m starting to learn different aspects about programming in kotlin. Sometimes I recive this confussin error message, I don’t understand it. can anybody explain me it??

    https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/c/cd4112d7518cc6bc9f05d0fb9b4db972c0b67616.png▾

    but if I open the generated class after...
  • r

    rss

    10/02/2025, 3:29 AM
    Accessibility iOS traversal order inconsistent? I’m not sure if I’m opening this in the right place but I’ve been banging my head against the wall for over a month on this I’ve got a compose scrollable column with some items, and I can’t make accessibility navigation go through them in the order they’re laid out - specifically on iOS. Android works perfectly whether I define traversalIndex or not. What’s worse is it’s inconsistent. The slower I traverse (with accessibility swipes) the better it works, but never 100% and eventually it just...
  • r

    rss

    10/02/2025, 4:09 PM
    Mismatch between glibc versions (Kotlin/Native on Linux) Consider the following example: Project foo is a KMP library with cinterop dependencies. The dependencies are built with system toolchain and target system libraries, e.g. glibc > 2.20 Project bar is a KMP / KN executable with a dependency on foo. When konan is invoked to build and link bar, it uses its own bundled LLVM and sysroot The sysroot bundled with konan (available in ~/.konan/dependencies) is heavily outdated (kernel 4.9.2, glibc 2.19) even though the LLVM toolchain is relatively...
  • r

    rss

    10/12/2025, 6:39 PM
    A syntax-level support for reactive ref type A reactive pipeline is very commonly used in various sicarios. In frontend development, we use such pipelines to bind the states and views in many ways. For example, we have ref and computed in Vue. We have re-executing mechanism in React and Compose. In server side development, we use streams and flows to process sequential data. However, to define the reactive pipeline is not that intuitive. For example, to define c=a+b, we have to write a lambda with explicit specifications. The lambda...
  • r

    rss

    10/13/2025, 7:59 AM
    Is it possible to configure demand delivery in KMP project I’m working on a Kotlin Multiplatform (KMP) project and want to explore whether it’s possible to configure on-demand feature delivery, similar to Android’s Play Feature Delivery or Play Asset Delivery. The idea is that we have a super app consisting of multiple modules say four independent apps/features and I want to separate each one so that when the user selects a specific app, it’s downloaded on demand and then opened directly within the same super app. I’d like to know: Can a KMP...
  • r

    rss

    10/13/2025, 10:19 AM
    I need guidence regarding navigation in KMP We have a native Android app that is mostly WebViews and uses Jetpack Navigation. We want to start migrating to fully native screens using Kotlin Multiplatform (KMP) to share both logic and UI between Android and iOS. None of us on the dev team has prior KMP experience. My assumption is the following: we could create a KMP module for, say, SSO, which uses <a href="https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-navigation-routing.html" rel="noopener nofollow ugc">Compose...
  • r

    rss

    10/15/2025, 6:39 AM
    C++ Library for Desktop Build I just started to learn KMP and I want to see how to integrate C++ code into my project. I successfully edited the build.gradle.kts to build a C++ library for Android and have called a JNI function in the library. // composeApp/build.gradle.kts android { ... externalNativeBuild { cmake { path = file("src/main/cpp/CMakeLists.txt") version = "3.22.1" } } But the desktop version is not able to do the same. If it helps. So far I added the...
  • r

    rss

    10/16/2025, 10:19 PM
    Delete / clear account I like to periodically scrub my online presence. I don’t mind the content going on to exist, but I would like the username to be replaced by [deleted account] or something similar. The profile page doesn’t allow me to edit things like user handle or delete the entire account. Please make that an option. 1 post - 1 participant Read full topic
  • r

    rss

    10/20/2025, 12:19 PM
    Errors running kotlin class from comd-line hello everyone, I’m trying to run a kotlin class from CLI(Kotlin command-line compiler | Kotlin Documentation), but I get some errors: <a class="lightbox" href="

    https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/f/f438507ef51a0277699e86512589ac5b9ca5d72f.png▾

    " data-download-href="/uploads/short-url/yQt9FkSlyIuDtl0KwFEiQ12KAgL.png?dl=1" title="image" rel="noopener...