Vivek Modi
10/16/2025, 7:22 AMBernhard
10/16/2025, 12:29 PMinline fun <reified T : Any> createEvent(
endpointTarget: String,
id: String,
type: String,
payload: T,
) = Event(
endpointTarget = endpointTarget,
id = id,
type = type,
payload = payload,
typeInfo = typeInfo<T>()
)
class Event<out T : Any> private constructor(
val endpointTarget: String,
val id: String,
val type: String,
val payload: T,
val typeInfo: TypeInfo,
)Bernhard
10/22/2025, 2:13 PMJoshua Hansen
10/22/2025, 6:32 PMclass Foo {
var number: Int = 0
}
Is it possible to pass a reference to the number property to another class so that the other class can reassign it?CLOVIS
10/22/2025, 6:46 PMinline fun <reified T> foo(): T .
This function calls a function fun <T> impl(type: KType): T? .
I want the behavior that:
• If foo is called with a nullable type (e.g. foo<Int?>()), then nulls can be returned.
• If foo is called with a non-nullable type (e.g. foo<Int>()), if impl() returns null then it throws an NPE.
Is this possible?
Usually I would use overloads when I have two functions that only differ from the nullability of their parameters/receivers, but I don't think this is possible when they only differ by return type?Kirill Grouchnikov
10/22/2025, 7:12 PMInt , passing in the encoded ARGB channels, is there a more "pleasant" way of replacing
Hct.fromInt(0xFFFF0000u.toInt())
with something closer to how it is in Java
Hct.fromInt(0xFFFF0000)
the trailing u.toInt() is just noise when I read this call. The only thing I can think of is adding my own fun Hct.fromLong extension to make it read better in Kotlin.Jakob
10/24/2025, 2:48 PMdata class TimeRange(
val from: Instant,
val to: Instant?,
) {
fun isOpenEnded(): Boolean {
return this.to == null
}
}
Optimally, I'd like to be able to use this helper and have the compiler still correctly infer whether to is null or not.
If I now add a method to check if a TimeRange ends later than another, it might look like this (it's logically incorrect but nevermind that for the sake of the example)
fun endsLaterThan(other: Timerange): Boolean {
return this.isOpenEnded() || (!other.isOpenEnded() && (this.to > other.to))
}
However, this code does not work as-is, the compiler gives me trouble about nullability. To my human eyes and brain it's clear that the final statement (<http://this.to|this.to> > <http://other.to|other.to>) is only reachable if neither <http://this.to|this.to> or <http://other.to|other.to> is null, since that would short circuit the boolean operations. The compiler does not infer this, so I thought I might be able to help it along using contracts!
I have tried to add something like this, but it does not seem to work since I'm using implies to say something about a member of the receiver and not just the receiver itself?
fun isOpenEnded(): Boolean {
contract {
returns(true) implies this.to == null
returns(false) implies this.to != null
}
return this.to == null
}
If there's a way to make this work, I'd love to know! Links to reading material about contracts in general is also appreciated 👍Jobby
10/26/2025, 8:41 AMBoo,Boo,Boo,Boo,Boo,Boo
Foo,Foo,Foo,Foo,Foo,Foo
Goo,Goo,Goo,Goo,Goo,Goo
This is my code together with the output I get as comments.
import java.io.File
fun main() {
val chars: String = File(
"C:\\Work\\Kotlin\\test\\src\\main\\resources\\MyTest.txt"
).readText(Charsets.UTF_8)
val lines = chars.split("\n")
println("Number of Lines: ${lines.size}") // Number of Lines: 3
println(lines[0]) // Boo,Boo,Boo,Boo,Boo,Boo
println(lines[1]) // Foo,Foo,Foo,Foo,Foo,Foo
println(lines[2]) // Goo,Goo,Goo,Goo,Goo,Goo
val myList1 = lines[0].split(",")
val myList2 = lines[1].split(",")
val myList3 = lines[2].split(",")
println("myList1: $myList1") // ]
println("Number of Words: ${myList1.size}") // Number of Words: 6
println("myList2: $myList2") // ]
println("Number of Words: ${myList2.size}") // Number of Words: 6
println("myList3: $myList3") // myList3: [Goo, Goo, Goo, Goo, Goo, Goo]
println("Number of Words: ${myList3.size}") // Number of Words: 6
}
As you can see it only outputs myList3 correctly. myList1 and myList2 just output ']'. But if you look at the size, it seems like it processed my string correctly.
And the thing is, this depends on which line is the last in the txt file. It just outputs the last line the way I'd expect, but not the previous ones.
Why is that?
Is this a bug?
JDK: corretto-23.0.2
Ide: IntelliJ IDEA 2025.1.5.1 (Community Edition) - Build #IC-251.28293.39, built on September 2, 2025Shaun Wild
10/26/2025, 2:59 PMPihentagy
10/27/2025, 3:04 PMwhen is uncomplete?
https://pl.kotl.in/uwOMcC-FdCLOVIS
10/27/2025, 8:07 PMimport io.ktor.http.HttpStatusCode.*
@Serializable
private data class NotFound(val id: String)
val f = foo<NotFound>(NotFound)
| |
| HttpStatusCode.NotFound
data class NotFoundy
10/29/2025, 2:25 PMfun <T> foo1(vararg elements: Iterable<T>) = /* ... */
I also have fun foo2(vararg elements: List<Bar>)
what exactly happens when I call foo1(*elements) from within foo2?
what does the spread operator do here? is this zero cost?Bernhard
10/30/2025, 9:40 AMgenerateSequence { Elem(it.hasNextPart())}.takeUntil {it.hasNext}
is there a better way to do that? and in that case, would you prefer an Iterator instead?Gasan
10/31/2025, 10:44 AMval? Kotlin does not have a concept of a "pointer to a const", therefore I can change the referenced value, unless it is immutable. I mean, val parameter variable does not amount to much unless the referenced value is immutable.ursus
11/01/2025, 12:50 AMy
11/03/2025, 9:44 AMCannot invoke "java.util.Map.get(Object)" because the return value of "A.getMethods()" is null
which we seem to get, but only sometimes.
we have this: (A, B, C are all concrete classes, renamed to anonymize)
abstract class A(/* ... */) : Lots, Of, Things {
abstract override val methods: Map<B?, C>
abstract override val someMethod: C?
/* ... */
override fun getMethods(): List<C> = methods.values + listOfNotNull(someMethod)
}
what could be the issue here?
is it an order of initialization thing?
is it related to the choice of the name getMethods? (since kotlin desugars getters to that name, no?)CLOVIS
11/03/2025, 5:12 PMJack Boyce
11/04/2025, 7:59 AMBernhard
11/04/2025, 11:58 AMhfhbd
11/05/2025, 11:14 AMnull in Map<String, String>.get(null): mapOf<String, String>("f" to "f").get(null as String?)Edgar Avuzi
11/08/2025, 5:52 AMelect
11/09/2025, 10:55 AMimport org.gradle.api.Project
import java.io.ByteArrayOutputStream
context(_: Project)
operator fun String.invoke(vararg args: String): String {
val output = ByteArrayOutputStream()
providers.exec { // error, `providers` unresolved
commandLine("arduino-cli", "version")
standardOutput = output
}
return output.toString()
}Rob Elliot
11/09/2025, 11:09 AMfun nullableReturn(): String? = TODO()
val timer: Timer = TODO()
try {
timer.recordCallable {
nullableReturn()
}
} finally {
timer.close()
}
> Argument type mismatch: actual type is 'CallableString?', but 'Callable<uninferred T! (of fun T : Any recordCallable)>!' was expected.Natalia Mishina
11/10/2025, 12:42 PMJérémy CROS
11/10/2025, 3:59 PMclass Provider {
private val listener by lazy {
object: 3rdPartyListener {
// Bunch of callbacks
// The one I actually care about
override fun onError() {
// Spam errors when in errors, do nothing otherwise
}
}
}
}
I've read about callbackFlow but I can't quite make it work.
For the use case of a callback that spam error / do nothing, AI is suggesting using a debounce but I don't know, that seems weird...
Any help / suggestions appreciated! 🙏Lukasz Kalnik
11/13/2025, 2:17 PMJoshua Hansen
11/17/2025, 7:40 PMAssigned value is never read on aNumber += 1 even with the suppressions. I personally consider this warning wrong but I'm sure people could argue about it. Is there anyway to suppress it?
Also, why did the `Wrapped into a reference object to be modified when captured in a closure." statement go away?
class Foo(val lambda: () -> Unit)
@Suppress("UNUSED_VARIABLE", "UNUSED")
fun test() {
var aNumber = 0 // "Wrapped into a reference object..." doesn't show anymore
val newFoo = Foo {
val test = 1 + aNumber // Do something with current value
aNumber += 1 // Change value to be used next time Foo.lambda() is called
}
// do something with newFoo here to use later like add it to a list, etc.
}Pihentagy
11/19/2025, 7:39 AMJoshua Hansen
11/19/2025, 7:25 PMinterface Test<T: Number> {
val clazz: KClass<T> // Keep reference to the class so we can find the right one later
fun foo(item: T)
}
// Int object:
object A : Test<Int> {
override val clazz = Int::class
override fun foo(item: Int) {
// do something
}
}
// Double object
object B : Test<Double> {
override val clazz = Double::class
override fun foo(item: Double) {
// do something
}
}
fun test() {
// Working with a list of these objects
val objs: List<Test<out Number>> = listOf(A, B)
val myInt = 1
// I'm certain find returns A, but due to forced out projection I get an error.
val properObj = objs.find {
it.clazz == myInt::class
}
properObj?.foo(myInt) // Receiver type 'Test<out Number>' contains out projection which prohibits the use of 'fun foo(item: T): Unit'.
}MrNiamh
11/20/2025, 3:47 PMa compile fine, but b is a compilation error? I wouldn't expect a to compile but it does, but i'm also surprised it's not consistent between the two.
data class ContactId(val value: UUID)
fun main(){
val contactId = ContactId(UUID.randomUUID())
val uuid = UUID.randomUUID()
val a = contactId == uuid
val b = contactId.toString() == uuid
}