Karlo Lozovina
03/30/2021, 1:23 PMSomeClass::class.companionObjectInstance
Tomasz Krakowiak
03/30/2021, 6:09 PMinterface ListModification<out E> {
fun applyTo(target: MutableList<in E>)
fun <T> apply(target: List<T>) : List<T>
where E : T
}
Dominick
03/31/2021, 4:01 AMval commands: MutableSet<Command<JDACommandSender>> = mutableSetOf()
get() = Collections.unmodifiableSet(field)
My goal is to allow the class itself to modify commands
directly, but others to only be able to get an unmodifiable version of the contents. Is there any direct way to achieve this?Marko Novakovic
03/31/2021, 7:24 AMfun interface Validation : (String) -> Boolean {
companion object
}
val Validation.Companion.validateEmail
get() = Validation { it.isNotEmpty() }
val Validation.Companion.validatePassword
get() = Validation { it.isNotEmpty() }
val isFormValid = Validation.validateEmail("email") && Validation.validatePassword("password")
and
typealias Validation = (String) -> Boolean
val validateEmail: Validation = { email: String -> email.isNotEmpty() }
val validatePassword: Validation = { password: String -> password.isNotEmpty() }
val isFormValid = validateEmail("email") && validatePassword("password")
What are use-cases where first one would add value over second one? Or is this just semantic difference?Andrew Ebling
03/31/2021, 8:54 AMsealed class
unsure if this is the most appropriate approach.dreamreal
03/31/2021, 1:34 PMuser
03/31/2021, 3:50 PMSami Eljabali
03/31/2021, 11:00 PMIntents.kt
@file:JvmName("IntentsUtil")
@file:JvmMultifileClass
open class Intents {
companion object
}
PlayStoreIntents.kt
@file:JvmName("IntentsUtil")
@file:JvmMultifileClass
fun Intents.Companion.viewAppOnPlayStore(appUri: String): Intent =
Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(appUri)
setPackage(Apps.PLAY_STORE.url)
}
When invoked Java side was able to simply:
IntentsUtil.viewAppOnPlayStore("foo");
Now however, it requires me to do:
IntentsUtil.viewAppOnPlayStore(Intents.Companion, "foo");
Any idea to why that is? How can I avoid passing the Companion object?Coen
04/01/2021, 8:27 AMGilles Barbier
04/01/2021, 4:01 PMval b = "1".toByteArray()
val s = mutableSetOf("1".toByteArray())
s.remove(b) // <== this does not work, probably because "1".toByteArray() != "1".toByteArray()
s.removeIf { b.contentEquals(it) } // <= this works
Rob Elliot
04/01/2021, 7:07 PMfun Runnable.toFun(): () -> Unit = { this.run() }
fun Function0<Unit>.toRunnable(): Runnable = Runnable { this() }
fun <T> Callable<T>.toFun(): () -> T = { this.call() }
fun <T> Function0<T>.toCallable(): Callable<T> = Callable { this() }
and then wondered a) do they already exist or b) is there a better way?Karlo Lozovina
04/02/2021, 8:57 AMSlackbot
04/02/2021, 5:53 PMelect
04/02/2021, 11:29 PM${'\n'}
the only way to add a new line in triple quotes?Andre Stefanov
04/03/2021, 7:33 PMSlackbot
04/04/2021, 4:08 AMchristophsturm
04/04/2021, 5:11 PMPeter
04/04/2021, 8:46 PMfun getProperties(t: Transaction) {
t::class.memberProperties.forEach {
println(it.name)
println(it.get(t)) // gives error since it doesn't expect type Transaction
}
}
Nikhil
04/05/2021, 11:17 AMFernando Avanzo
04/05/2021, 2:49 PMv79
04/05/2021, 2:51 PMhisham bin awiad
04/05/2021, 2:51 PMnkiesel
04/05/2021, 5:11 PMval logger: Logger = LoggerFactory.getLogger(Foo::class.java)
in the companion object of a class Foo
. I remember a discussion about defining this as a class property instead of as a companion object property (which would then allow to use val logger: Logger = LoggerFactory.getLogger(this::class.java)
) but I can't find it anymore. What is current best practice here ?nkiesel
04/05/2021, 6:01 PMgetFoo
which returns an object of type Foo
(and never returns null
) is Foo!
. Should I (a) ignore this and use val foo = getFoo()
(b) use val foo: Foo = getFoo()
or (c) val foo = getFoo()!!
Sandeep Chandra
04/06/2021, 9:22 AMSandeep Chandra
04/06/2021, 9:23 AMthhh
04/06/2021, 1:07 PMCody Mikol
04/06/2021, 8:12 PMYuri Drigin
04/07/2021, 8:54 AMjava.lang.AssertionError: Enum class does not have .values() function
when make enum Serializable
Kotlin 1.4.31
Canary 13
Custom Serializer for this enum doesn’t help, (((dave08
04/07/2021, 9:11 AMdata class Foo(val id: Int, val name: String)
What's an ideomatic way of searching for an id
and replacing in-place the entry with another name (without turning the list into a map)? And return the id or the found entry as a bonus..dave08
04/07/2021, 9:11 AMdata class Foo(val id: Int, val name: String)
What's an ideomatic way of searching for an id
and replacing in-place the entry with another name (without turning the list into a map)? And return the id or the found entry as a bonus..Big Chungus
04/07/2021, 9:14 AMdave08
04/07/2021, 9:15 AMarekolek
04/07/2021, 9:15 AMdave08
04/07/2021, 9:15 AMBig Chungus
04/07/2021, 9:16 AMdave08
04/07/2021, 9:17 AMarekolek
04/07/2021, 9:18 AMAnd return the id or the found entry as a bonusdid you mean the old
name
maybe? why return id
if it's going to be the same as the passed argument or null (if not found)?dave08
04/07/2021, 9:18 AMarekolek
04/07/2021, 9:20 AMdave08
04/07/2021, 9:22 AMarekolek
04/07/2021, 9:25 AMfun MutableList<Foo>.replace(id: Int, name: String): Foo? {
val index = indexOfFirst { it.id == id }
return if (index > -1) {
set(index, get(index).copy(name = name))
} else {
null
}
}
dave08
04/07/2021, 9:27 AMarekolek
04/07/2021, 9:34 AMfun <T> MutableList<T>.replace(predicate: (T) -> Boolean, transformation: (T) -> T): T? {
val index = indexOfFirst(predicate)
return if (index > -1) {
set(index, transformation(get(index)))
} else {
null
}
}
fun MutableList<Foo>.replace(id: Int, name: String): Foo? {
return replace({ it.id == id }) { it.copy(name = name) }
}
Big Chungus
04/07/2021, 9:37 AMdave08
04/07/2021, 9:38 AMBig Chungus
04/07/2021, 9:41 AMarekolek
04/07/2021, 9:49 AMEven better yet, crossinline lambda args toodoes that change anything in the case above?
Big Chungus
04/07/2021, 9:51 AMarekolek
04/07/2021, 10:52 AMinline
does? bytecode seems to be exactly the same with and without crossinline
ephemient
04/07/2021, 3:56 PMarekolek
04/07/2021, 7:14 PMephemient
04/07/2021, 7:50 PMdave08
04/08/2021, 10:00 AMfun <T> MutableList<T>.modify(predicate: (T) -> Boolean, transformation: T.() -> Unit): T? {
val iterator = listIterator()
while (iterator.hasNext()) {
val value = iterator.next()
if (predicate(value)) return value.also { value.transformation() }
}
return null
}