Philipp von der Linden
05/26/2025, 7:33 PMRaman Chahal
05/28/2025, 2:43 AMYhcs Ami
05/29/2025, 7:59 AMAnuj Chahal
05/29/2025, 1:39 PMStephan Schröder
06/01/2025, 10:25 AMMichael Krussel
06/02/2025, 6:43 PMclassDeclaration (used by declaration)
: modifiers? ('class' | ('fun'? 'interface'))
simpleIdentifier typeParameters?
primaryConstructor?
(':' delegationSpecifiers)?
typeConstraints?
(classBody | enumClassBody)?
;
I would expect this to work, but it doesn't
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.Manasseh
06/04/2025, 3:34 AMKlitos Kyriacou
06/04/2025, 2:51 PMimport 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
).hfhbd
06/04/2025, 5:03 PMis Foo.A, is Foo.B
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)
}
}
Mark
06/05/2025, 7:48 AM@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`:
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:
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.Manasseh
06/05/2025, 6:18 PM{{#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:
mapOf("tables" to this.tableNames, "adminName" to configuration.adminName)
Klitos Kyriacou
06/06/2025, 5:25 PMclass 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
}
}
CLOVIS
06/06/2025, 7:24 PMclass 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:
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.y
06/08/2025, 12:53 PMsealed 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
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
06/09/2025, 8:18 AMclass 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?y
06/10/2025, 6:18 AMdata 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.CLOVIS
06/14/2025, 10:21 AMsimon
06/17/2025, 9:30 AMCLOVIS
06/18/2025, 8:04 AMclass Foo<out T>
I need to create a function
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
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?y
06/18/2025, 3:30 PMinterface HasFoo {
val foo: Foo
}
I want something like
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
...?)الخبرات العلمية ScienceExperts
06/20/2025, 8:12 AMالخبرات العلمية ScienceExperts
06/20/2025, 2:36 PMSamuel
06/21/2025, 7:30 PMval (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 seemsJoel Denke
06/23/2025, 7:13 PMPablichjenkov
06/24/2025, 3:28 AM1.8.22
to 1.9.25
and I am noticing some changes I haven’t heard of. I have the following Java definition:
public abstract class Abc<T> {
void function1(@NonNull T t)
}
In Kotlin 1.8.22, below code compiles without issues.
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:
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?
class AbcSubclass2<T : Any> : Abc<T> ...
Stephan Schröder
06/24/2025, 10:01 PMjvmToolchain(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:
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?Lukas K-G
06/25/2025, 8:01 AMalexhelder
06/26/2025, 1:23 AMvalue class DownloadManagerId(val value:Long)
is there a way to “unwrap” this without having to say downloadManagerId.value
at each use?Hong Phuc
06/26/2025, 1:49 AMUserServices
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 advanceJérémy CROS
06/26/2025, 1:31 PMprivate 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:
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! 😊