182630g
03/18/2020, 2:52 AMAjinkya Saswade
03/18/2020, 3:04 PMSteve
03/18/2020, 7:51 PMCzar
03/19/2020, 6:11 PMfun Sequence<Int>.isOrderedAscending(): Boolean =
zipWithNext { prev, next -> prev <= next }
.fold(true) { acc, value -> acc && value }
Ivan Brko
03/20/2020, 10:06 AMgabin
03/20/2020, 4:53 PMMichael Saragih
03/21/2020, 9:22 AMkeochris
03/21/2020, 9:25 AMiseki
03/22/2020, 5:17 AM<T>
to <Base<T>>
?
open class C<T:C<T>>{
fun <R> foo(a:R){
a as T // looks not works...
}
}
Ray Ryan
03/23/2020, 8:42 PMclass Outer {
abstract class Inner {
}
}
class Child extends Outer.Inner {
Child(Outer outer) {
// Calls Inner constructor, providing
// outer as the containing instance
outer.super();
}
}
I can’t find an equivalent in Kotlin, if I want Child
to live in a separate file / module.
The auto converter generates this, which doesn’t compile:
class Outer {
abstract inner class Inner
}
class Child(outer: Outer?) : Inner()
zero_coding
03/24/2020, 1:29 PMGilberto Diaz
03/24/2020, 2:43 PMfun Int.add(){}
. I’m trying to find docs about this approach but can’t find it.Victor Cardona
03/24/2020, 10:45 PMGus
03/26/2020, 4:24 PModay
03/30/2020, 2:51 PMGus
03/30/2020, 5:03 PMopen class TheCompanion(val foo: String)
open class Parent {
fun doIt(): String {
return foo
}
companion object : TheCompanion("the-parent")
}
class Child1() : Parent() {}
class Child2() : Parent() {
companion object : TheCompanion("child2")
}
Child1().doIt()
Child2().doIt()
How can I get Child2().doIt()
to output "child2"
instead of "the-parent"
?
I guess this isn't possible because there's no way to enforce that subclasses of Parent
must have a companion object implementing a given interface, right?Giovanni Attina
04/01/2020, 1:27 AMIvan Brko
04/01/2020, 11:15 AModay
04/01/2020, 1:59 PMprivate fun getActiveFilters(categoryId: Int): String? {
val activeFilters = mutableFilterItems.value?.mapNotNull {
it.takeIf { filterItem ->
filterItem.categoryId == categoryId && filterItem.toggled
}
}?.joinToString { it.itemValue.capitalize() }
return activeFilters
}
oday
04/01/2020, 1:59 PMactiveFilters
is null or empty, I want to return null, otherwise I want to return activeFiltersoday
04/01/2020, 1:59 PMharry.singh
04/07/2020, 10:52 PMplusAssign
operator function always Unit
?Ben
04/08/2020, 10:54 AMif (getFoo() != null && barEnabled()) {
doSomething();
}
In Kotlin I could write
getFoo().takeIf { barEnabled() }?.let { doSomething() }
but I'm not convinced that's any easier to read, mostly because of the separation between the nullable getFoo() and the let. Any recommendations on how best to write that in Kotlin? Is using scope functions always preferred?ursus
04/09/2020, 3:26 AMursus
04/09/2020, 3:28 AMvinny2020
04/09/2020, 6:40 PMIlya Pogrebenko
04/10/2020, 8:37 AMobject Consts {
const val KOTLIN = "kotlin"
}
Consts::class.memberProperties.forEach { println(it.get(???)) }
What should I pass as a receiver?Ananiya
04/11/2020, 5:34 AMAnaniya
04/11/2020, 11:33 AMChris Cordero
04/11/2020, 9:37 PMList<Int>
that I want to map over and do some calculations on. Since I'm learning Kotlin, I thought I'd try to do it two ways. This one works:
val foo: List<Int> = listOf(100, 200, 300)
foo.map { it / 3 - 2 } .forEach { println(it) }
However, I wanted to see what to do if I wanted to make a reusable transform function inside the map
. So I tried this:
fun myMapFunc(it: Int): Int {
return it / 3 - 2
}
val foo: List<Int> = listOf(100, 200, 300)
foo.map(myMapFunc).forEach { println(it) } // error
I already know I can fix it by doing this:
foo.map { myMapFunc(it) }
But I feel like the first way should work too... Why doesn't it?Chris Cordero
04/11/2020, 9:37 PMList<Int>
that I want to map over and do some calculations on. Since I'm learning Kotlin, I thought I'd try to do it two ways. This one works:
val foo: List<Int> = listOf(100, 200, 300)
foo.map { it / 3 - 2 } .forEach { println(it) }
However, I wanted to see what to do if I wanted to make a reusable transform function inside the map
. So I tried this:
fun myMapFunc(it: Int): Int {
return it / 3 - 2
}
val foo: List<Int> = listOf(100, 200, 300)
foo.map(myMapFunc).forEach { println(it) } // error
I already know I can fix it by doing this:
foo.map { myMapFunc(it) }
But I feel like the first way should work too... Why doesn't it?Shawn
04/11/2020, 9:49 PMfoo.map(::myMapFunc)
Chris Cordero
04/11/2020, 9:50 PM::
doing?::
means that it is "creating a member reference or class reference", but I'm not sure what that means here exactly.Shawn
04/11/2020, 9:55 PMfoo(Bar)
foo(Bar::class)
which will generate a KClass<Bar>
for you and send that to the function::
will actually let you reference a class’s companion object
if one is defined on it::
is kinda like using *
to get a pointer to something in C/C++Chris Cordero
04/11/2020, 10:04 PM