Landry Norris
08/02/2022, 6:30 PMyield
is a suspend function, so it can only be called within a suspend function or other coroutine context. inner
is not marked as suspend, so it can’t call suspend functions.Byeol
08/03/2022, 11:14 AMRomão
08/03/2022, 9:09 PMRomão
08/03/2022, 9:13 PMtseisel
08/05/2022, 8:07 PMprettier
, which IMHO formats code in a cleaner way than those 2 Kotlin tools. ktlint looks promising, but I don't like that it also lint-checks code ; I'd expect it to focus on formatting. Any Kotlin alternative?martmists
08/06/2022, 4:10 AMapplication
gradle plugin, how do I stop an application? The stop button is greyed out...
If I close the tab, it simply keeps running in the backgroundColton Idle
08/06/2022, 5:34 AMfun getBooks(): Flow<List<Book>> {
return callbackFlow {
val listener =
FirebaseFirestore.getInstance().collection("books").addSnapshotListener { value, error -> trySend(value to error) }
awaitClose { listener.remove() }
}
.doSomethingWithNullValueAndError()
.conflate()
.map { (value, _) ->
value.toObjects()
}
.flowOn(defaultDispatcher)
rednifre
08/06/2022, 1:03 PMbind
extension function on all nullable types, that is only available inside of a nullable
block.
I tried to do it like this, but the bind
function is not reachable. What’s the proper way to do this?
class BindNullableException : IllegalAccessException()
fun interface NullableBlock<A> : () -> A {
fun <A> A?.bind() = this ?: throw BindNullableException()
}
fun <A> nullable(block: NullableBlock<A>): A? =
try {
block()
} catch (e: BindNullableException) {
null
}
fun main() {
val a = getNullableA().bind() // should not work
val nullableC = nullable {
val a = getNullableA().bind() // should work
val b = getNullableB().bind()
getNullableC(a, b)
}
}
fun getNullableA(): String? = null
fun getNullableB(): String? = null
fun getNullableC(a: String, b: String): String? = null
Lokik Soni
08/06/2022, 3:45 PMbatteryProfile(Unit).flowWithLifecycle(lifecycle).collect { result ->
val level = alertLevel(Unit).firstOrNull()?.data ?: Constants.DEF_VAL_LEVEL
}
Ayfri
08/06/2022, 11:53 PMKotlin
module option, but else I can't find how to create a new empty Kotlin module with gradle 🤔Abdullah Samir
08/07/2022, 2:37 PMAbdullah Samir
08/07/2022, 2:38 PMephemient
08/07/2022, 7:01 PMMuhammad Talha
08/08/2022, 5:37 AMjanvladimirmostert
08/08/2022, 10:28 AMquery<Int, String>(sql: String)
so that it will output O2<Int, String>
since it has two output types or if it's query<Int, Int, Int>
outputting O3<Int, Int, Int>
etc or query<T1,T2,...,T99>(sql: String)
should return O99<T1,T2,...,T99>
Alternatively, query(O(Int, String)
should output O<Int, String>
How would you go about designing such an API?
I've tried a few things which I've posted on SO, I've implemented the "alternatively" part, but ran into the problem where I'm outputting O2<Int.Companion, String.Companion>
instead of O2<Int, String>
which I could probably solve by doing query(O(InstanceOfInt, InstanceOfString)
, but that already looks terrible.
https://stackoverflow.com/questions/73271850/make-kotlin-function-output-tnk1-kn-when-the-input-is-tnk1-companion-kn-comDavid Smith
08/09/2022, 9:23 AMUnit
has to return Unit
? By being able to return anything when a Unit
is expected I have created a bug in my code where some piece of side-effecting code wasn’t “run” to return Unit
but the compiler didn’t pick it up because you can return anything if Unit
is expected. 😞thanksforallthefish
08/09/2022, 12:34 PMRyan Smith
08/10/2022, 12:36 AMelse
is there to be exhaustive, but is a no-op
?
e.g.
when {
thing is OneThing -> // do something
thing is SomethingElse -> // do something else
else -> { /* no-op */ }
}
It doesn't exactly fit this case from the style guide especially since the app is still under development and there could be more cases:
Prefer usingIs a barefor binary conditions instead ofif
when
return
better? Or just sticking with no-op
? Personal preference?phldavies
08/10/2022, 9:19 AMx?.let { ... }
to if (x != null) { … }
in the coding conventions. Am I right in thinking the preference of ?.let
would be when the result is used as an expression, but if (!= null)
is better served for control-flow?Ben Edwards
08/10/2022, 7:36 PMpoohbar
08/10/2022, 9:47 PMopen class Animal<T1, T2>
class Cat<I> : Animal<I, List<I>>()
val x: Animal<String, List<String>> = Cat()
however trying to do the same instantiation in java fails:
Animal<String, List<String>> x = new Cat<String>() // error
Is there something I can do to improve the generics in the Kotlin code so that they can be used from Java just like from Kotlin?Stefan Oltmann
08/11/2022, 9:17 AMBen Edwards
08/11/2022, 10:33 AMBen Edwards
08/11/2022, 10:36 AMjuh juh
08/11/2022, 11:32 AMhfhbd
08/11/2022, 12:53 PM!(foo == "FOO")
should refactored into this foo != "FOO"
.
Is it possible to use the Kotlin plugin/frontend and its Kotlin fixup automatically? (I know this would require mapping my internal representation to Kotlin frontend code) Or do you know some other library which has basic optimizations? Alternative I have to write these Kotlin optimization by myselfrednifre
08/11/2022, 1:12 PMclark
08/11/2022, 2:53 PM@Target(AnnotationTarget.FUNCTION)
annotation class DefaultErrorMessage(val defaultErrorMessage: String)
@DefaultErrorMessage("This is a test")
fun testErrorMessage() {
// How do I access the DefaultErrorMessage?
}
Also, would it be possible to access that message from a function called by the annotated function for example:
@Target(AnnotationTarget.FUNCTION)
annotation class DefaultErrorMessage(val defaultErrorMessage: String)
@DefaultErrorMessage("This is a test")
fun testErrorMessage() {
testNestedAccess()
}
fun testNestedAccess() {
// How do I access default error message?
}
oday
08/11/2022, 2:56 PMif the values in this list are valid values in my enum
?
I am doing this
if (queryParams.containsAll(GiveConsent.ConsentType.values())){
but i get this error that it expects a collection and I gave it an ArrayBen Edwards
08/11/2022, 3:10 PMval arranged: Boolean = ((h1 >= h2 && h2 >= h3) || (h1 <= h2 && h2 <= h3))
I get effectively marked down for adding the brackets for readability.
So is this really affecting performance negatively?
I'm a big fan of readability 🙂
Am I missing something?.Ben Edwards
08/11/2022, 3:10 PMval arranged: Boolean = ((h1 >= h2 && h2 >= h3) || (h1 <= h2 && h2 <= h3))
I get effectively marked down for adding the brackets for readability.
So is this really affecting performance negatively?
I'm a big fan of readability 🙂
Am I missing something?.Chris Lee
08/11/2022, 3:16 PMMichael de Kaste
08/11/2022, 3:17 PM((h1 >= h2 && h2 >= h3) || (h1 <= h2 && h2 <= h3)).let(::println)
Casey Brooks
08/11/2022, 3:19 PMval arranged: Boolean = (h1 >= h2 && h2 >= h3) || (h1 <= h2 && h2 <= h3)
which I think makes it more readable than having them there
the &&
operator has higher precedence than ||
so the inner parentheses are technically unnecessary, but I find it can help with readability, especially for folks that aren’t comfortable with operator precedence. So I would personally keep those ones in this expressionBen Edwards
08/11/2022, 3:23 PMVampire
08/11/2022, 4:41 PMval arranged: Boolean = ((h1 >= h2) && (h2 >= h3)) || ((h1 <= h2) && (h2 <= h3))
That's also what the "add clarifying parentheses" in Java code does, and what it also does in Kotlin now iirc.Ben Edwards
08/11/2022, 4:44 PMVampire
08/11/2022, 4:50 PM<=
and &&
is easier to remember or deduct, you still have to remember it and also first read half of the line to understand the expression.
With my / IntelliJs variant, it is instantly obvious what the sub-expressions are and you can just read it from left to right and right away understand it.
It is simply simpler to grasp and understand and thus reduces brain-load.val arranged: Boolean = (h2 in (h3..h1)) || (h2 in (h1..h3))
Roukanken
08/12/2022, 7:57 AM<=
then it's time to split it
val isDescending: Boolean = (h1 >= h2) && (h2 >= h3)
val isAscending: Boolean = (h1 <= h2) && (h2 <= h3)
val arranged: Boolean = isDescending || isAscending
Unnecessary nested parenthesis just obscure which ones are the pairs. Yes, IDE helps with that, or stuff like Rainbow brackets plugin, but it still denies you the just knowing start/end pairs on first glanceVampire
08/12/2022, 9:41 AM