kotlin
Does Kotlin provide any performance boosts? [closed]
Kotlin generates very similar bytecode to Java, so the performance of Kotlin code is in most cases the same as the performance of the equivalent Java code. One way in which Kotlin can be faster than Java is inline functions. With inline functions, code using higher-order functions such as filter or map can be compiled … Read more
Android Kotlin Coroutines: what is the difference between flow, callbackFlow, channelFlow,… other flow constructors
For callbackFlow: You cannot use emit() as the simple Flow (because it’s a suspend function) inside a callback. Therefore the callbackFlow offers you a synchronized way to do it with the trySend() option. Example: fun observeData() = flow { myAwesomeInterface.addListener{ result -> emit(result) // NOT ALLOWED } } So, coroutines offer you the option of … Read more
How do you make a text clickable in jetpack compose ? I would also like to toggle it to non clickable after selecting once
You can add the clickable modifier to your Text or use ClickableText instead of Text. Here is an example of how to do it with ClickableText: var enabled by remember { mutableStateOf(true)} ClickableText( text = AnnotatedString(text) , onClick = { if (enabled) { enabled = false text = “Disabled” } })
Test if string contains anything from an array of strings (kotlin)
You can use the filter function to leave only those keywords contained in content: val match = keywords.filter { it in content } Here match is a List<String>. If you want to get an array in the result, you can add .toTypedArray() call. in operator in the expression it in content is the same as … Read more
assign variable only if it is null
The shortest way I can think of is indeed using the elvis operator: value = value ?: newValue If you do this often, an alternative is to use a delegated property, which only stores the value if its null: class Once<T> { private var value: T? = null operator fun getValue(thisRef: Any?, property: KProperty<*>): T? … Read more
Is there a way to reference the Java class for a Kotlin top-level function?
Another way I found is to declare a local class or an anonymous object inside a top level function and to get its enclosingClass: val topLevelClass = object{}.javaClass.enclosingClass Note: to work, this declaration should be placed on top level or inside a top-level function. Then you can use the topLevelClass as a Class<out Any>: fun … Read more
How to send request body in spring-boot web client?
You’re not setting the “Content-Type” request header, so you need to append .contentType(MediaType.APPLICATION_JSON) to the request building part.
What’s the use of Moshi’s Kotlin codegen?
This is sort of three questions Why is code gen useful Code gen is useful as a compile-time alternative to the reflective moshi-kotlin. Both of them are useful because they natively understand Kotlin code and its language features. Without them, Moshi would not be able to understand Kotlin nullability, default values, and much more. There … Read more