Marko Novakovic
04/19/2022, 4:42 PMOleg Siboglov
04/19/2022, 5:41 PMinline
modifier for functions. The Kotlin lang webpage for inline function states “Inlining may cause the generated code to grow. However, if you do it in a reasonable way (avoiding inlining large functions), it will pay off in performance, especially at “megamorphic” call-sites inside loops.” When it states that we should avoid inlining large functions, does this refer solely to the function receiving the inline
modifier or does it apply to the contents of the lambda (which is being passed to the inlined function) as well?eygraber
04/19/2022, 9:12 PMColton Idle
04/20/2022, 6:37 AMCountry(code=AD, name=Andorra)
but I need to convert it into json. Is there a stringify method or something I can use from kotlin stdlib in android? Most questions I look up on stackoverflow want me to bring in gson, but I really dont want to bring in a depedency for just this single little manipulation. Am I missing something basic here?Alex
04/20/2022, 11:15 AMlucapette
04/20/2022, 2:58 PMMark
04/21/2022, 4:21 AMIterable
and returns true if and only if all items return the same value for a given lambda? I don't care what the value is. I know I can just apply lambda to first item and then use Iterable.all {}
but I was wondering if there was a nicer way.Cristina Uroz
04/22/2022, 9:56 AMMR3Y
04/22/2022, 3:42 PMFractalParent
that is declared as follows:
internal interface FractalParent : FractalNavScope {
// ...
val zoomAnimationSpecFactory: () -> AnimationSpec<Float>
// ...
}
and the class that implements the interface is:
internal class FractalNavStateImpl : FractalParent {
// ...
override lateinit var zoomAnimationSpecFactory: () -> AnimationSpec<Float>
my question is how he was able to override zoomAnimationSpecFactory
and change its modifier from val
to lateinit var
without getting a compilation error?Jan
04/23/2022, 3:50 PMAnthony Flores
04/24/2022, 5:19 PMclass SomeClass {
val listOfThing = Array<Thing>()
val thing1 = Thing()
fun Thing.invoke() {
listOfThing.add(this)
}
}
hooliooo
04/25/2022, 8:46 AMStylianos Gakis
04/25/2022, 1:31 PMfun foo(lambda: Bar.() -> Unit)
. On one of the places where I call this, I happen to be inside a suspending function, where I want to be able to simply call on a suspend function like I normally would. This isn’t allowed since that lambda isn’t suspending, and I can’t make it suspending either. I see that this is solved in the vast majority of cases by having the function itself be inline
which makes this possible, however I see that it’s not possible to define an interface function as inline.
I’m sure this is something that others have faced as well, what are my options? I see that this is solved by defining them as extension functions to the interface like map is defined, but in my case I can’t do that either since the implementation of my interface wants to use some local variables when implementing this function.Muhammad Talha
04/26/2022, 6:18 AMVitali Plagov
04/26/2022, 9:31 AMobject TestConfig {
val ENV_UNDER_TEST by lazy { ... }
}
It’s a global variable I’m using in the project that stores the environment name under test. Is it possible to change that variable during the runtime? maybe with reflection or somehow else? I do understand that it’s not supposed to be changed and that it’s a dirty hack, so I accept that risk.Colton Idle
04/26/2022, 6:42 PMenum class BackendEnv(val humanReadableName: String) {
STAGING("Staging"),
PROD("Production")
}
How would you do it?Ayfri
04/26/2022, 8:59 PMinternal set
?Goldcrest
04/27/2022, 3:32 AMval nonNullVal =
if(condition1) {
someOperation.get() // null
} else if (condition2) {
someOperationTwo.get() // null
} else {
null
} ?: defaultVal
When condition 1 is true our nonNullVal
evaluates to null
.
When condition 1 is false and condition 2 is true our nonNullVal
evaluates to defaultVal
What is happening? X_x I would just like to get a grasp of what is going on internally.
Any help is appreciated! Thanks!René
04/27/2022, 7:08 AMclass Sample<T> {
@Nullable
public T getValue() {...}
protected void setValue(T value) {...}
//...
}
That is the simplified base class I had in mind something like:
class Foo<T> : Sample<T>() {
override val `value`: T
get() {...}
set(value) {...}
}
Sam Smallman
04/27/2022, 9:47 AMRené
04/27/2022, 12:53 PM@set:JvmName("setMyFoo")
@get:JvmName("getMyFoo")
open var foo: Int = 42
without open
it worksrrva
04/27/2022, 3:59 PMAnthony Flores
04/28/2022, 9:45 PMis
with generics?
For example if the generic is <T : SomeClass>
how can check if T is a specific child of SomeClass
?Mark
04/29/2022, 1:35 AMinterface Sequenceable<out T> {
fun asSequence(): Sequence<T>
}
interface Item
interface TypeOfItem: Item
interface Items: Sequenceable<Item>
interface TypeOfItems : Items, Sequenceable<TypeOfItem> // Error message: Type parameter T of 'Sequenceable' has inconsistent values: Item, TypeOfItem
https://pl.kotl.in/BSez9qCNJJan
04/29/2022, 2:09 PMdata class AttachmentItem(val imageId: String, val extension: String)
now I have two lists of these attachments:
val list1 = listOf<AttachmentItem>()
val list2 = listOf<AttachmentItem>()
//now I want to know which AttachmentItems are missing in list1 but are there in list 2 and vice versa. So:
val diff1 = //attachments available in list1 but not in list2
val diff2 = //attachments available in list2 but not in list1
How would I do that in kotlin?Shumilin Alexandr
04/29/2022, 8:01 PMShumilin Alexandr
04/30/2022, 8:43 PMdata class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {}
function:
fun test(date1: MyDate, date2: MyDate) {
// this code should compile:
println(date1 < date2)
}
Problem: I need to write a method for comparing MyDate objects
I write a solution, it works ok
override fun compareTo(other: MyDate): Int {
val yearDiff = year - other.year
val monthDiff = month - other.month
val dayDiff = dayOfMonth - other.dayOfMonth
if (yearDiff == 0) {
if (monthDiff == 0) {
if (dayDiff == 0) {
return 0
}
} else return monthDiff
} else return yearDiff
return 0;
}
and now “good” solution:
override fun compareTo(other: MyDate) = when {
year != other.year -> year - other.year
month != other.month -> month - other.month
else -> dayOfMonth - other.dayOfMonth
}
i am not understand how works “good” solution? can you help me?
what does mean
year != other.year -> year - other.year
Tariyel Islami
05/01/2022, 7:18 PMTianyu Zhu
05/01/2022, 10:07 PMmyFlow.collect { /* some work */ }
, is that work being done concurrently? If so, on how many threads? How do I control it?Alan Lee
05/03/2022, 6:29 AMPriorityQueue
and compareBy
?
fun testPQ(words: Array<String>) {
val heap = PriorityQueue<String>(
compareBy<String>{ it.length }
)
words.forEach {
heap.offer(it)
}
println(heap)
}
fun main() {
val input = arrayOf("the","day","is","sunny","tha","a","def","sunny","as","by")
testPQ(input)
}
The result I see is [a, as, is, day, by, the, def, sunny, sunny, tha]
by
and tha
seems to be out of order.Alan Lee
05/03/2022, 6:29 AMPriorityQueue
and compareBy
?
fun testPQ(words: Array<String>) {
val heap = PriorityQueue<String>(
compareBy<String>{ it.length }
)
words.forEach {
heap.offer(it)
}
println(heap)
}
fun main() {
val input = arrayOf("the","day","is","sunny","tha","a","def","sunny","as","by")
testPQ(input)
}
The result I see is [a, as, is, day, by, the, def, sunny, sunny, tha]
by
and tha
seems to be out of order.ephemient
05/03/2022, 6:41 AMAlan Lee
05/03/2022, 7:35 AM