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

    ursus

    08/11/2025, 1:12 PM
    After updaing kotlin to 2.2 (probably), when building android release with gradle I'm gettinga million of
    Copy code
    Info: Unexpected error while reading XYZ's kotlin.Metadata: element 'k' is missing.
    Internet says to
    -keep class kotlin.Metadata { *; }
    , about which I'm unsure about I never had to mess with kotlin's default proguard keep rules Should I do it? Anyone else experiencing it?
    c
    • 2
    • 3
  • c

    Colton Idle

    08/11/2025, 1:28 PM
    Looking at some kotlin code and found a data class being declared, and inside of it there's a companion object that builds the data class. That seems smelly right. Does it actually break any "rule"... like "data classes are data classes, and not [blank]" is what i want to say. lol
    😆 1
    r
    j
    +4
    • 7
    • 13
  • k

    Kelvin Chung

    08/11/2025, 3:26 PM
    Question: is there a way to mark an interface as "can only be implemented by a certain type and its subclasses"? Better yet, is there an alternate way to express the intent? I'm trying to wrap my hands around something like:
    Copy code
    interface Extender<T> {
      context(thisValue: T)
      fun doSomething()
    }
    
    class Foo : Extender<Foo> {
      context(thisValue: Foo)
      override fun doSomething() { ... }
      
      // So, Foo would need a context-free overload of doSomething() anyways
      // passing itself as context, in order for outsiders to use it?
      fun doSomething() = context(this) { doSomething() }
    }
    j
    n
    • 3
    • 14
  • e

    Eric Joseph

    08/11/2025, 5:52 PM
    Hey! Is there a native Kotlin function like dropWhile that doesn’t create a new list?
    n
    r
    +2
    • 5
    • 14
  • m

    Matt Kessler

    08/12/2025, 11:37 AM
    Hi. 👋 I'm trying to write a basic "Hello World" type app that can run on Android, iOS, Desktop, and Web. I am following this guide... https://www.jetbrains.com/help/kotlin-multiplatform-dev/quickstart.html Given that I'm developing on 🪟, I did the web KMP wizard which seemed to work well enough. Without much fuss, I have it running on Android and I next wanted to try Desktop but I ran into a problem. The tutorial suggests that there will be a composeApp [desktop] run configuration but I don't see that.
    s
    • 2
    • 12
  • k

    Kelvin Chung

    08/13/2025, 12:12 AM
    Hey folks. I need some help in diagnosing a test failure possibly related to design. Suppose I have this:
    Copy code
    interface MyType
    
    value class Wrapper(value: Int) : MyType, WrapperAddOp
    
    interface WrapperAddOp : MyType {
      operator fun plus(rhs: Wrapper): Wrapper = // default implementation
    }
    When I try to run a test (JVM), I get a
    NoSuchMethodError
    when I try to invoke the
    plus
    method on
    Wrapper
    , saying that a mangled name starting with
    plus
    on
    Wrapper
    doesn't exist. Anyone with any insights on this?
    n
    • 2
    • 22
  • r

    Rafael Costa

    08/13/2025, 9:52 AM
    Hey guys 👋 I'm trying context parameters, but failing in this case:
    Copy code
    fun bar(
        lambda: context(String) () -> Unit
    ) {
        with("") {
            lambda()
        }
    }
    
    fun foo() {
        bar {
            this // error
        }
    }
    Am I missing something? Is this not supposed to give me a string? I am not able to get the context parameter of the lambda in any way. If I try
    bar { str ->
    then "str" here is also an error 🤔
    j
    y
    • 3
    • 11
  • j

    Jeff Hudson

    08/13/2025, 7:56 PM
    Java's
    CyclicBarrier
    is really nice, but using it from Kotlin is not nice, since it's not coroutine-friendly. I've searched high and low, and haven't found a good alternative other than to roll my own. Has anyone run into this?
    ✅ 1
    a
    • 2
    • 5
  • c

    Colton Idle

    08/14/2025, 12:45 PM
    do kotlin data classes have no arg constructors? I'm asking because of gson documentation "If you are using ProGuard or R8 (for example for Android projects) you might not need any special Gson configuration anymore if your classes have a no-args constructor and use @SerializedName for their fields."
    c
    r
    +2
    • 5
    • 11
  • d

    Daniel Pitts

    08/14/2025, 3:44 PM
    I used the AI Self-Review feature, and it gave me this:
    Copy code
    The inner `None` object, used in the `Maybe` class to represent an absent value, is defined using `data object`. Although this is suitable for describing uniqueness and immutability, `data object` is over-specified here, as it incurs unnecessary memory allocation inefficiency compared to plain `object`. Consider replacing `data object None` with `object None`.
    This seems like its wrong...
    s
    y
    +2
    • 5
    • 20
  • v

    Vivek Modi

    08/18/2025, 12:58 PM
    Hi everyone, I’m looking for an example of handling offline data. Specifically, I want to store data fetched from the network and display it in the UI. If it’s the first time, the app should fetch data from the network; otherwise, it should use the stored data. I’d also like to know the best way to handle data synchronization — for example, when a user saves new data while offline, it should automatically sync to the server once the network is available. This should work across android, iOS and desktop in a multiplatform setup.
    not kotlin but kotlin colored 3
    c
    • 2
    • 2
  • c

    CLOVIS

    08/18/2025, 1:48 PM
    I'm trying to make sense of the following error message:
    Copy code
    Kotlin: Argument type mismatch: actual type is 'Select<CapturedType(out Record!)#1>', but 'Select<out CapturedType(out Record!)#2!>!' was expected.
    This is triggered by the code:
    Copy code
    val a: Select<out Record?> = TODO()
    val b: Select<out Record?> = TODO()
    a.unionAll(b)
    where
    Select
    is declared in Java:
    Copy code
    public interface Select<R extends Record>
    extends
        ResultQuery<R>,
        TableLike<R>,
        FieldLike,
        FieldOrRowOrSelect
    {
    
        @NotNull @CheckReturnValue
        @Support
        Select<R> unionAll(Select<? extends R> select);
    
    }
    Shouldn't
    <? extends R>
    allow
    R
    itself?
  • h

    Hunter

    08/18/2025, 4:33 PM
    I just ran into an issue where I can't use a
    value class
    as a
    vararg
    parameter. Is there a reason for this? I don't understand why this would be a limitation.
    e
    • 2
    • 2
  • p

    purezen

    08/18/2025, 6:36 PM
    Just completed the codelab from the official course to learn about View Models
  • p

    purezen

    08/18/2025, 6:36 PM
    It used the inbuilt (?)
    ViewModel
    library
  • p

    purezen

    08/18/2025, 6:37 PM
    Do I need to learn Hilt or Koin now before proceeding to make an app ?
    c
    s
    • 3
    • 4
  • c

    Colton Idle

    08/18/2025, 6:44 PM
    I'm on a new project and it's insanely complex. I almost wish I can print out method names as they are called in order to help me see what methods are called on a button click for example. Is there any easy way to do that? So far I'm manually going in and doing multicursor search for
    fun
    and then putting a log statement. It works just takes a while.
    j
    a
    +2
    • 5
    • 8
  • r

    Robert Jaros

    08/19/2025, 2:03 PM
    I'm trying to fill Kotlin Developer Survey 2025 and I've got an error on the Kotlin Multiplatform page - there seems to be a required question/field I don't see and I can't proceed. Anyone else is having this problem?
    ✅ 1
    m
    d
    +2
    • 5
    • 10
  • a

    Adam S

    08/20/2025, 5:15 AM
    Why can the type of an overridden property be changed to a subtype, but only if the property is a
    val
    , not a
    var
    ?
    Copy code
    sealed interface ParentType
    class SubTypeA : ParentType
    class SubTypeB : ParentType
    
    sealed interface DataHolder1 {
      var data: ParentType
    
      // ERROR: Type of 'data' doesn't match the type of the overridden 'var' property 'var data: ParentType' defined in 'DataHolder1'.
      class HolderA(override var data: SubTypeA) : DataHolder1
      // ERROR: Type of 'data' doesn't match the type of the overridden 'var' property 'var data: ParentType' defined in 'DataHolder1'.
      class HolderB(override var data: SubTypeB) : DataHolder1
    }
    
    sealed interface DataHolder2 {
      val data: ParentType
    
      // but the type of `val` properties can be changed?
      class HolderA(override val data: SubTypeA) : DataHolder2
      class HolderB(override val data: SubTypeB) : DataHolder2
    }
    https://pl.kotl.in/esiw_FX5H
    • 1
    • 1
  • t

    timrijckaert

    08/20/2025, 2:24 PM
    I'm trying to migrate from context-receivers to context-parameters and found this guide. https://blog.jetbrains.com/kotlin/2025/04/update-on-context-parameters/ I followed the guide for _Smooth Transition_ing, I replaced the freeCompilerArg
    -Xcontext-receivers
    with
    -Xcontext-parameters
    and resynced. I don't get to see the new inspection. Neither in the latest Android Studio stable/beta nor IntelliJ Ultimate. What am I missing?
    Copy code
    IntelliJ IDEA 2025.2 (Ultimate Edition)
    Build #IU-252.23892.409, built on August 1, 2025
    Source revision: 0a9a69b3f9332
    w
    • 2
    • 1
  • n

    Nicolai C.

    08/21/2025, 3:51 AM
    I'm having a small issue with functional/SAM interface, and I'm not sure if this is intended behavior, and if it is I don't understand why. If I convert this to an object extending
    Example
    and manually implement
    example1
    I can reference
    example2
    just fine, which puzzles me as to why the following snippet doesn't compile.
    Copy code
    val example = Example {
    	// this is without `example1`
    	example2() // this line doesn't compile
    }
    
    fun interface Example {
    	fun example1()
    
    	fun example2() {
    		println("Example")
    	}
    }
    d
    e
    y
    • 4
    • 5
  • t

    Teodora Mihaila

    08/21/2025, 10:12 AM
    Hey everyone! I'm working on some code changes and have been getting an error re: the Kotlin Gradle Plugin. What is the appropriate channel to ask my question in? The specific error I've been getting when upgrading the language version from 1.9.25 to 2.2.0 is:
    Copy code
    Caused by: java.lang.NoSuchMethodError: 'org.jetbrains.kotlin.buildtools.api.jvm.ClasspathEntrySnapshot org.jetbrains.kotlin.buildtools.api.CompilationService.calculateClasspathSnapshot(java.io.File, org.jetbrains.kotlin.buildtools.api.jvm.ClassSnapshotGranularity, boolean)'
    k
    • 2
    • 2
  • m

    Mi Re

    08/21/2025, 12:18 PM
    Hi, I need some help 🙂 I'm working on a KMP project targeting Android and iOS, and I think the issue might be with my Kotlin code, so I hope someone can help me here. I'm having trouble signing out using Firebase Auth from GitLiveApp/firebase-kotlin-sdk. When I call onSignOutClicked in
    ProfileScreen
    , my app navigates to
    AuthScreen
    using the
    signOut()
    callback passed as a constructor argument to
    ProfileScreen
    . After that, my
    AuthViewModel
    collects
    authService.currentUser
    the same way it’s done in
    ProfileViewModel
    inside the
    init
    block. However, it still returns a non-null
    FirebaseUser
    , even though I already signed out. I’ll attach some code in a reply. Thanks in advance!
    • 1
    • 1
  • g

    Giorgi

    08/22/2025, 9:54 AM
    Hi. probably not the right channel but why are comments turned off on this video

    https://www.youtube.com/watch?v=vWIDRH6aQfI▾

    ?
    t
    • 2
    • 2
  • f

    Fernando

    08/22/2025, 3:25 PM
    Hello! Quick question regarding formatting in compose and IntelliJ. Is there a plugin/tool that formats the code in the editor (i.e. ⇧+alt+⌘ + L) and a CLI tool to ensure consistency?
    c
    • 2
    • 4
  • r

    robstoll

    08/25/2025, 9:43 AM
    can you define a context parameter for lambdas? something like
    Copy code
    foo(f: context(users: UserService) Bar.() -> Unit)
    👌 1
    h
    h
    • 3
    • 10
  • b

    bazbo.222

    08/25/2025, 4:24 PM
    Hey! I know Kotlin has lazy properties with its delegates feature. Is there any way to add lazy evaluation (or, equivalently for my use case, memoization) such that the first time I call
    foo(arg)
    ,
    foo(arg)
    gets calculated, but the second, third etc. times that I call
    foo
    after that,
    foo(arg)
    just returns the already computed, stored value. E.g. in Python, this exists in the form of the
    @Memoize
    operator, if anyone is familiar with that. Is there any way to achieve this in Kotlin or should I just implement the memoization mechanism myself.
    j
    y
    j
    • 4
    • 8
  • j

    Joshua Hansen

    08/25/2025, 10:55 PM
    Is it possible to access an extension property given this setup?
    Copy code
    fun main() {
        val book = Book("Kotlin Basics", List(10) { Page(it + 1) })
        
        val p = book.pages[4] // get the fullName from this scope given `book` and `p`?
    }
    
    
    class Book(val name: String, val pages: List<Page>) {
        val Page.fullName: String
            get() = "$name: Page $number of ${pages.size}"
    }
    
    class Page(val number: Int)
    y
    k
    • 3
    • 3
  • h

    Huib Donkers

    08/26/2025, 11:13 AM
    I suppose this is very niche..: Can I initialize class member `val`s from a lambda while also forbidding non-local returns in that lambda? • If I want to allow non-local returns, I can do this: playground example. Using the non-local return in this case actually triggers an error for kotlin 2 (not for 1.9.25), but for a different reason. It's easy to give an example where the non-local return is allowed also in kotlin 2. • marking the lambda
    crossinline
    , forbids the non-local return, but I can no longer initialize member `val`s from the lambda. I can initialize member `var`s, and local `val`s. See playground example here. I don't understand why initializing member
    val
    would no longer be possible. ◦ When trying to initialize a member
    val
    kotlin 1.9.25 errors "Captured values initialization is forbidden due to possible reassignment" (is the contract not understood here?) and kotlin 2 errors "Initialization of captured member values must be inlined" (is it not inlined?)
  • y

    y

    08/26/2025, 11:37 AM
    hey, we're seeing
    Cannot invoke "java.util.Map.get(Object)" because the return value of foo() is null
    very naively:
    foo()
    is a Kotlin function which returns
    List<Bar>
    , so under what conditions could it even return null?
    h
    • 2
    • 1