https://kotlinlang.org logo
Join Slack
Powered by
# getting-started
  • p

    Philipp von der Linden

    05/26/2025, 7:33 PM
    As I have just arrived I wish to express my excitement and gratitude. I hope for inspiring exchange of ideas.
    👋 1
    kodee welcoming 5
    👍 2
  • r

    Raman Chahal

    05/28/2025, 2:43 AM
    Hi team! I'm new to Android development and just getting started. I've learned a few basics in Jetpack Compose, I need more resources to continue learning. It would be awesome if you could share some good ones. I'd really appreciate it!
    m
    n
    d
    • 4
    • 3
  • y

    Yhcs Ami

    05/29/2025, 7:59 AM
    In the latest Android Studio version (Meerkat Feature Drop 2024.3), I wrote a Dice Roller program following the online tutorial, but when running it on a virtual simulation device, there was no response when clicking a button with the mouse. Then when I connected to my Android phone, I was able to interact normally by pressing buttons. So, may I ask, is there something set incorrectly? I tried simulating a switch on the device called 'Hardware Input', but it didn't work.
    g
    r
    • 3
    • 3
  • a

    Anuj Chahal

    05/29/2025, 1:39 PM
    Hello everyone, I am new to this community. I recently started learning Kotlin for my android development (in February) and I am struggling so much in finding a roadmap and the learning resources for becoming a complete android developer. Please help me. And please make sure the resources are complete latest and free.
    kodee welcoming 1
    l
    k
    +2
    • 5
    • 16
  • s

    Stephan Schröder

    06/01/2025, 10:25 AM
    anyone else noticed that the New language features on the way - article on the Kotlin blog contains 2 error in its 6 points? "Positional destructuring" isn't new, and "Must-use return values" is the same as "CheckReturnValue". So I assume (at least parts of) the article was written by AI and nobody (with enough technical knowledge) proofread it. Can someone fix this? It's not a good look that this is still wrong on the main Kotlin blog after a whole week.
    😲 5
    ✅ 1
    p
    l
    +3
    • 6
    • 7
  • m

    Michael Krussel

    06/02/2025, 6:43 PM
    I'm feeling stupid, but I cannot find an example for what the syntax is for what I want to do. I'm trying to create generic class, where the generic type is two lower limits and the class implements an interface via delegation. From the grammer
    Copy code
    classDeclaration (used by declaration)
      : modifiers? ('class' | ('fun'? 'interface'))
        simpleIdentifier typeParameters?
        primaryConstructor?
        (':' delegationSpecifiers)?
        typeConstraints?
        (classBody | enumClassBody)?
      ;
    I would expect this to work, but it doesn't
    Copy code
    class Foo<T>(t: T, bar: Bar) : Bar by bar where T: T1, T : T2 {}
    I've tried a few variants, adding commas or colon between the by bar and the where. If I remove the
    by
    it compiles.
    y
    e
    k
    • 4
    • 7
  • m

    Manasseh

    06/04/2025, 3:34 AM
    Hi guys! So I'm building this library I want to test in a different project locally. I published the artifacts to the local maven repo (.m2/repository) and added the dependency in my build script. The build runs correctly and I can actually start the other project (which is a server-side app), but I'm unable to actually import the library (e.g. import xyz.daimones.ktor.Class;). What could I be doing wrong?
    s
    r
    • 3
    • 24
  • k

    Klitos Kyriacou

    06/04/2025, 2:51 PM
    Is there any way to combine annotation classes, for example like this:
    Copy code
    import kotlin.reflect.full.hasAnnotation
    
    annotation class Annotation1
    annotation class Annotation2
    
    @Annotation1
    @Annotation2
    annotation class CombinedAnnotation
    
    @CombinedAnnotation class X
    
    fun main() {
        println(X::class.hasAnnotation<Annotation1>())
    }
    That above prints "false". My inspiration is from Spring, which annotates the
    @Service
    annotation with
    @Component
    and the doc says that the former is a specialization of the latter (so every class annotated with
    @Service
    is treated as if it was also a
    @Component
    ).
    e
    • 2
    • 2
  • h

    hfhbd

    06/04/2025, 5:03 PM
    Why is the compiler not able to smart cast the member s to Int in this case:
    is Foo.A, is Foo.B
    Copy code
    public sealed interface Foo {
        public val s: Int?
        
        public data class A(override val s: Int): Foo
        public data class B(override val s: Int): Foo
        public data class C(override val s: Int?): Foo
    }
    
    public fun a(foo: Foo) {
        when (foo){
            is Foo.A -> foo.s + foo.s // smart cast to A and s is Int
            is Foo.B -> foo.s + foo.s // smart cast to B and s is Int
            is Foo.C -> (foo.s ?: 0) + (foo.s ?: 0) // smart cast to C, but s is Int?
        }
        when (foo){
            is Foo.A, is Foo.B -> foo.s + foo.s // no smart cast, Foo.s is Int?
            is Foo.C -> (foo.s ?: 0) + (foo.s ?: 0)
        }
    }
    e
    • 2
    • 6
  • m

    Mark

    06/05/2025, 7:48 AM
    I have a fairly complex data model where the base interface has a
    @Composable operator fun invoke(): String = …
    function. One of the sub interfaces is a
    fun interface
    and I was getting this runtime error whenever
    invoke()
    was called on an instance of that `fun interface`:
    Copy code
    java.lang.AbstractMethodError: abstract method "java.lang.String BaseInterface.invoke(androidx.compose.runtime.Composer, int)"
    Using a standard
    interface
    instead of a
    fun interface
    solved the problem. AI reckons:
    Copy code
    The error occurred because the fun interface was trying to create a synthetic method that conflicted with the @Composable invoke() method from the parent interface. By making it a regular interface, we ensure proper inheritance of all methods from BaseInterface.
    Is this a known issue? I can’t post the full data model here because it’s too much code, so I hope this is enough info.
    d
    s
    • 3
    • 6
  • m

    Manasseh

    06/05/2025, 6:18 PM
    Please how do I loop through an array of strings in handlebars/mustache? I have this but it doesn't work
    Copy code
    {{#tables}}
         <li class="list-none font-bold text-base text-white p-4 cursor-pointer">
                <a class="no-underline text-white" href="#">{{this}}</a>
         </li>
    {{/tables}}
    Assusming this model is passed to it:
    Copy code
    mapOf("tables" to this.tableNames, "adminName" to configuration.adminName)
    not kotlin but kotlin colored 3
    o
    • 2
    • 14
  • k

    Klitos Kyriacou

    06/06/2025, 5:25 PM
    How can I call the extension function when I have a member function of the same name but taking a differently-typed function parameter?
    Copy code
    class C {
        fun foo(block: Int.() -> Unit) {
            123.block()
        }
    }
    
    fun C.foo(block: String.() -> Unit) {
        "hello".block()
    }
    
    fun main() {
        val c = C()
        c.foo {
            println((this as String).length)  // this is Int, want String
        }
    }
    m
    r
    • 3
    • 2
  • c

    CLOVIS

    06/06/2025, 7:24 PM
    I have a class that looks like:
    Copy code
    class Foo(
        val a: Byte,
        val b: Byte,
    ) {
        constructor(bytes: ByteArray) : this(bytes[0], bytes[1])
    }
    The problem with this implementation is that if
    bytes
    is too small, the error message is not perfectly clear, and if it is too big, there is no error message at all. I can add an error message like so:
    Copy code
    constructor(bytes: ByteArray) : this(bytes[0], bytes[1]) {
            require(bytes.size == 2) { "…" }
        }
    but that only protects against the case where the array is too large. How can I add some code that runs before
    : this(…)
    to add the check? The constructor must remain a constructor.
    r
    g
    +2
    • 5
    • 12
  • y

    y

    06/08/2025, 12:53 PM
    sorry that this is convoluted, but it's pretty close to the real code:
    Copy code
    sealed class Base() {
        abstract val bar: Bar
        
        fun interface Bar {
            fun frobnicate(a: Int, b: String)
        }
    }
    
    class Derived : Base() {
        override val bar: Bar get() = ::DC // ERROR: Return type mismatch: expected 'Base.Bar', actual 'Function2<Int, String, Unit>'.
    }
    
    data class DC(val a: Int, val b: String)
    vs
    Copy code
    sealed class Base(val bar: Bar) {
        fun interface Bar {
            fun frobnicate(a: Int, b: String)
        }
    }
    
    class Derived : Base(::DC) // works
    
    data class DC(val a: Int, val b: String)
    the second one compiles. how do I get the first one to compile? do I require an explicit cast to
    Bar
    ? EDIT: is it because `fun interface`s get "promoted" when passed directly like this?
    y
    • 2
    • 4
  • y

    y

    06/09/2025, 8:18 AM
    I have a module with many different classes, and I have a
    class AST
    which contains some combination of these. I want to compare two instances of
    AST
    for equality. the catch is that there's also a particular property that may exist in each class, which should be ignored for this comparison. what is the least painful way to do this?
    h
    k
    • 3
    • 8
  • y

    y

    06/10/2025, 6:18 AM
    continued the discussion here.
    data class
    has a feature where you opt-out of automatic generation of
    equals()
    (etc.) by moving a property outside of the primary constructor... this (imo) is a good feature. but
    copy()
    prohibits a non-public primary constructor for a
    data class
    , so how do you even exclude anything that needs to be set at init? do I really need to fall back to manual implementation any time I have such a property? you can use a default value for such properties (or even
    lateinit var
    ), and initialize them to a non-default value via a secondary constructor. but since the primary constructor is public this seems rather error-prone.
    l
    k
    v
    • 4
    • 9
  • c

    CLOVIS

    06/14/2025, 10:21 AM
    Is there a way to reinterpret a signed integer to unsigned without changing its in-memory representation? (and the other way around as well)
    p
    k
    l
    • 4
    • 24
  • s

    simon

    06/17/2025, 9:30 AM
    Is there anything like an inverse of the flatten function for a list? I want to take a list and turn it into a list of pairs (0, 1), (2,3) .. etc
    r
    • 2
    • 2
  • c

    CLOVIS

    06/18/2025, 8:04 AM
    I have a
    Copy code
    class Foo<out T>
    I need to create a function
    Copy code
    fun <T> Foo<T>.foo(o: Foo<T>)
    that function should NOT compile if the two
    T
    are different. As it is, it does compile because
    T
    is inferred to
    Any?
    . Currently, I use
    @OnlyInputTypes
    , but that's very brittle and I'd prefer to migrate to something more stable. I have tried
    Copy code
    fun <T, R : T> Foo<T>.foo(o: Foo<R>)
    but
    T
    also gets inferred to
    Any?
    . Is there another way to declare that constraint?
    s
    a
    y
    • 4
    • 13
  • y

    y

    06/18/2025, 3:30 PM
    I have an interface
    Copy code
    interface HasFoo { 
        val foo: Foo
    }
    I want something like
    Copy code
    sealed interface UpdateableFoo : Foo {
        fun updateFoo(newFoo: Foo): Self
    }
    where
    Self
    is a self type (not supported in Kotlin, as per my understanding). what's the least messy way of doing this? should I just not use an interface, and use an extension function
    fun <T: HasFoo> updateFoo(newFoo: Foo): T
    instead? (edit: actually, I don't think I can do that, since
    HasFoo
    isn't sealed and so I can't enumerate the `HasFoo`s inside
    updateFoo
    ...?)
    e
    y
    • 3
    • 3
  • u

    الخبرات العلمية ScienceExperts

    06/20/2025, 8:12 AM
    i have problem i upgrade to M4 but android studio keep spinning some time i had to force it to quit what should i do ?
  • u

    الخبرات العلمية ScienceExperts

    06/20/2025, 2:36 PM
    i ahave another question for web should we use kmp or koobweb ? i need something ready for production or should continue with react i need to create CMS dashboard ?
    a
    • 2
    • 1
  • s

    Samuel

    06/21/2025, 7:30 PM
    How do I destructure declarations with tuples? Attempt:
    Copy code
    val (a, b) = if (s) (true, "actual ") else (false, "deny ")
    I mean I guess I could use
    listOf
    but is there a better way? EDIT: Ohhh
    Pair
    is what I should be using it seems
    s
    l
    h
    • 4
    • 12
  • j

    Joel Denke

    06/23/2025, 7:13 PM
    https://kotlinlang.org/docs/whatsnew22.html#kotlin-compiler-unified-management-of-compiler-warnings Does this mean I can put deprecation as information now? I want information but not warning, because using warning as errors. Or if possible mark as warning but only that one not warning as error?
  • p

    Pablichjenkov

    06/24/2025, 3:28 AM
    Did a recent update in a project from Kotlin
    1.8.22
    to
    1.9.25
    and I am noticing some changes I haven’t heard of. I have the following Java definition:
    Copy code
    public abstract class Abc<T> {
      void function1(@NonNull T t)
    }
    In Kotlin 1.8.22, below code compiles without issues.
    Copy code
    class AbcSubclass1<T> : Abc<T> {
      override fun function1(T t) = Unit
    }
    But in Kotlin 1.9.25, I get a type mismatch error. Then the compiler/IDE suggest the following code which compiles perfectly:
    Copy code
    class AbcSubclass2<T> : Abc<T> {
      override fun function1(T & Any t) = Unit
    }
    Notice the type
    T
    vs
    T & Any
    , it seems like a new language enhancement to typing. Am I right? Is this the best fix or Perhaps defining the class like below is better?
    Copy code
    class AbcSubclass2<T : Any> : Abc<T> ...
    s
    y
    • 3
    • 7
  • s

    Stephan Schröder

    06/24/2025, 10:01 PM
    So I interpreted the line "Kotlin 2.2.0 brings many updates to the JVM. The compiler now supports Java 24 bytecode and ..." in What's new in Kotlin 2.2 to mean that I can compile my Kotlin project to Java 24 bytecode, but when I try to do just that (via
    jvmToolchain(24)
    and using Kotlin 2.2.0, Gradle 8.14.1, and Amazon's 24.0.1 Corretto JDK a
    ./gradlew clean build
    gives me:
    Copy code
    Kotlin does not yet support 24 JDK target, falling back to Kotlin JVM_22 JVM target
    Did I understand something wrong, or is this a bug?
    h
    s
    e
    • 4
    • 7
  • l

    Lukas K-G

    06/25/2025, 8:01 AM
    Is there some documentation stating which kotlinx serialization plugin version to use with witch Kotlin version? I can't seem to find anything in the official documentation.
    s
    c
    d
    • 4
    • 11
  • a

    alexhelder

    06/26/2025, 1:23 AM
    if I have a value class like
    value class DownloadManagerId(val value:Long)
    is there a way to “unwrap” this without having to say
    downloadManagerId.value
    at each use?
    s
    • 2
    • 1
  • h

    Hong Phuc

    06/26/2025, 1:49 AM
    Hi all, I’m currently having a class call
    UserServices
    that contains all the CRUD operations for the
    User
    resource in my application. Is it a good idea to include functionalities like
    LoginUser
    and
    LogoutUser
    in the same
    UserServices
    class. I know that these functions belong to a different domains (Authentication vs CRUD), but since I’m trying to create resource-based api, I’m not sure where else to put these auth functions. Does anyone have any insight on how I should handle this? Thanks in advance
  • j

    Jérémy CROS

    06/26/2025, 1:31 PM
    Hi everyone 🙂 Kinda noobish question regarding concurrent list access... Basically, we have a mutable list that can be updated from multiple threads so we've added a mutex to protect against issues, something like that:
    Copy code
    private val list = mutableListOf<T>()
    private val mutex = Mutex()
    
    override suspend fun addData(data: List<T>) {
        mutex.withLock {
            list.add(data)
        }
    }
    Every once in a while, we're saving this data to a file in a string format so we have:
    Copy code
    private fun getLog(data: List<T>): String = data.mapNotNull { it.format() }.joinToString(" ")
    (here, we're passing the above mutable list as parameter data) And this particular function sometimes (rarely) generates crashlytics reports with the exception
    ConcurrentModificationException
    So, 2 questions really 😅 How can this function throw this particular exception when, as far as I can tell, it's not modifying anything? And, what's the best way to make this more robust? Thanks! 😊
    s
    e
    +3
    • 6
    • 19