robstoll
08/25/2025, 9:43 AMfoo(f: context(users: UserService) Bar.() -> Unit)
bazbo.222
08/25/2025, 4:24 PMfoo(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.Joshua Hansen
08/25/2025, 10:55 PMfun 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)
Huib Donkers
08/26/2025, 11:13 AMcrossinline
, 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
08/26/2025, 11:37 AMCannot 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?purezen
08/26/2025, 1:27 PMpurezen
08/26/2025, 1:27 PMimport kotlinx.coroutines.*
fun main() {
println("Weather forecast")
delay(1000)
println("Sunny")
}
purezen
08/26/2025, 1:28 PMpurezen
08/26/2025, 1:28 PMUberunix
08/27/2025, 12:39 AMUberunix
08/27/2025, 2:24 AMArjan van Wieringen
08/30/2025, 9:16 AMv79
08/30/2025, 9:15 PMJohn Safwat
09/01/2025, 12:27 PMRob Elliot
09/02/2025, 9:54 AMimport java.time.Period
fun Period.toDays() = when (this) {
Period.ofYears(1) -> 365
Period.ofMonths(1) -> 30
else -> days
}
the check is an equality check, not an identity check, isn't it?
I'm getting a compiler warning Identity-sensitive operation on an instance of value type 'Period' may cause unexpected behavior or errors.
, but surely this is an equality check...Vampire
09/03/2025, 5:09 PMfun <T : Any> foo(block: () -> T?) {}
fun <T : Any> foo(block: () -> List<T>?) {}
fun <K : Any, V : Any> foo(block: () -> Map<K, V>?) {}
fun <T : Any> foo(block: () -> Set<T>?) {}
foo { "foo" }
foo { listOf("foo") }
foo { mapOf("foo" to "bar") }
foo { setOf("foo") }
(And without casting the arguments, I mean that the right method is used automatically)Vampire
09/03/2025, 9:59 PMfoo
is non-null?
So why is the second foo
call not working?
fun foo(foo: Any) = null
val foo: String? = null
foo(foo)
check(foo != null)
require(foo != null)
assert(foo != null)
foo(foo)
Slackbot
09/04/2025, 5:41 AMOdinakachukwu Omaka-chukwu
09/04/2025, 12:40 PMsealed interface RandomState {
data object Empty: RandomState
enum class Validity(val message: String): RandomState {
Valid("Is valid"),
Invalid("Is not valid"),
}
}
fun foo(state: RandomState) {
when(state) {
RandomState.Empty -> Unit
RandomState.Validity.Valid -> {
showMessage(state.message)
}
RandomState.Validity.Invalid -> {
showMessage(state.message)
}
}
}
Why is it not able to smart cast to RandomState.Validity.Valid
and RandomState.Validity.Invalid
?ursus
09/05/2025, 11:50 PMfun foo(): String {
var local: String? = null
if (local == null) {
local = "hello"
}
return local // compiler still thinks nullable
}
Why doesn't this smart cast?Dewan Tawsif
09/07/2025, 4:26 PMAozen
09/08/2025, 12:03 PMFredrik Meyer
09/10/2025, 6:27 AMGateway
and a class parameter KClass<Gateway>
. But when I pass an instance of KClass<GatewayImpl>
, the compiler complains. Is there a way around this? Casting works, so something is correct.Hildebrandt Tobias
09/10/2025, 7:56 AM2025-09-02T10:59:38+02:00
and at one point (envelope) I deserialize it as Instant
with no problem.
At another point (payload) I want to do the same but it doesn't work. I looked at the imports but both import kotlinx.datetime.Instant
.
But one does so from org.jetbrains.kotlinx:kotlinx-datetime
and the other from org.jetbrains.kotlinx:kotlinx-datetime-jvm
, with the latter not working.
The only dependency I have in both my common
and jvmBackend
modules is org.jetbrains.kotlinx:kotlinx-datetime
so I guess it selects which one to use by itself.
What would be the best course of action to remedy this Problem?
Edit: Seems like I have to use Instant.parse()
, before I had the json serializer do it, since most payloads are json and a select few are plain strings, but before I had no problems
just using the json parser for that and it allowed me to have a more generic approach for all messages.df
09/10/2025, 1:43 PMigor.wojda
09/10/2025, 4:48 PMJoshua Hansen
09/12/2025, 4:45 PMval
at all somewhere other that its declaration site.
class Test {
private val test: String
init {
test = "Hello"
}
}
Gasan
09/17/2025, 5:02 PMJoe
09/17/2025, 11:12 PM[WARNING] file:///home/jbarnett/workspace/tribe/leakycauldron/graphql/src/main/kotlin/com/trib3/graphql/execution/LeakyCauldronHooks.kt:50:30 This declaration overrides a deprecated member but is not marked as deprecated itself. Add the '@Deprecated' annotation or suppress the diagnostic.
... but gives no such warning in 2.2.10 (I mostly only care because -Werror
). it looks like it is marked deprecated itself unless I'm missing something? I can, of course, override the non-deprecated methods instead but wanted to understand what's going on.min
09/18/2025, 7:31 AMthis.getChildAt(0)
returns View!
should it always be guarded with an if
like this?
if (childCount > 0) {
getChildAt(0) as? AndroidComposeView
}
Why can’t I just do getChildAt(0) as? AndroidComposeView
for an AndroidComposeView?
that is null
if the View!
was null?