Travis Griggs
03/15/2021, 7:20 PMval activity:KClass<FragmentActivity>
get() {
return when (this) {
Single, SingleWithSense, SingleWithPeriod, Dual, Echo -> AllocatedNodeActivity::class
Central -> CentralNodeActivity::class
Unknown -> PristineNodeActivity::class
}
}
It just yells at me because none of the return types match the KClass<FragmentActivity> type. What's the secret sauce I'm missing?bbaldino
03/15/2021, 7:43 PMTravis Griggs
03/15/2021, 10:20 PMtherealbluepandabear
03/16/2021, 2:39 AMprivate fun extractCommand(command: String): Boolean {
val asArr = command.split("\\s")
if (asArr[0].equals(".attack", ignoreCase = true) && asArr.size == 3)
return true
}
Is there a way to do this with regex? I think that's the best waySlackbot
03/16/2021, 3:37 PMBenoît
03/16/2021, 5:30 PMjeggy
03/16/2021, 5:49 PMdata class MyType(
/**
* Information [testing...]
*/
val field: String,
)
I'm not sure if I have found a Kotlin bug or it's supposed to do this?
But this data class results in a compiler error because of the [testing...]
comment.Travis Griggs
03/16/2021, 6:45 PMParameter 'whatever' is never used
warnings for polymorphic methods, where in some cases, you really don't need to use the argument, but it's there for polymorphism. What's the idiomatic way to deal with this? I apparently cannot use `_`; I don't understand why.TwoClocks
03/16/2021, 11:40 PMval
in init
or in a constructor, but not both? What's the reasoning behind this code not compiling? https://pl.kotl.in/lVxY8idN4Fredrik Larsen
03/17/2021, 10:12 AMoperator
invoke?
I’m having an issue where it’s only accessible within the same (gradle) module.
operator fun Foo.invoke(block: FooDsl.() -> Unit) { ... }
fun Bar.invoke(block: BarDsl.() -> Unit) { ... }
I.e. the following does not work from another module
foo { // cannot resolve
}
However, this does
bar.invoke {
}
There are no declarations of invoke in the classes in question.
And for completeness this also works from another module
foo.invoke {
}
melatonina
03/17/2021, 12:29 PMmelatonina
03/17/2021, 5:35 PMinterface X<T> {
operator fun plus(n: Int) : T
fun next(): T = this + 1
}
inline class A(val value: Int) : X<A> {
override operator fun plus(n: Int) = A(value + n)
}
causes
Exception in thread "main" java.lang.ClassCastException: class com.example.A cannot be cast to class java.lang.Number (com.example.A is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')
at com.example.A.next-m5RZA0A(Example.kt:32)
at com.example.ExampleKt.test(Example.kt:5)
This version causes the same exception:
interface X<T> {
fun add(n: Int) : T
fun next(): T = add(1)
}
inline class A(val value: Int) : X<A> {
override fun add(n: Int) = A(value + n)
}
This version is OK:
interface X<T> {
fun next(): T
}
inline class A(val value: Int) : X<A> {
override fun next() = A(value + 1)
}
This version is OK, too:
interface X<T> {
fun next(): T
}
inline class A(val value: Int) : X<A> {
operator fun plus(n: Int) = A(value + n)
override fun next() = this + 1
}
Does it have anything to do with boxing? Is this because the plus operator is called on a reference of an interface?
Is this something limited to mathematical operators?
What should I do? Should I prefer code generation instead of interface implementation when using inline classes?Gerard Bosch
03/17/2021, 6:38 PMfun <T> T?.onNull(effects: () -> Unit): T? {
if (this == null) effects()
return this
}
What I feel strange is that by defining the above extension, I'm allowed to invoke it on non-nullable
types, which results a little counter-intuitive. I mean, given the above I can do:
val str: String = "helo" // non-nullable type
str.onNull { print("Will never print as I'm not actually null") } // <-- Why the compiler allows this?
Is it possible to restrict it just to nullables as in T?
Thx 🙂Fábio Carneiro
03/17/2021, 9:15 PMTravis Griggs
03/17/2021, 11:32 PMstatus
and a newState
values, and I'd like to match on combinations. I could of course nest them, but I was hoping to match on a tuple like thing...
e.g.
when (status, newState) {
42, 13 -> "The answer is bad luck"
}therealbluepandabear
03/18/2021, 3:41 AMprivate var worldSize: Int = 0
private var biomes: MutableMap<Biome, IntRange> = mutableMapOf()
data class PointData(val currentPoint: Int?, val currentBiome: Biome)
fun getPointData(point: Int): PointData {
biomes.values.forEach {
if (it.contains(point)) {
return PointData(point, biomes.keys.elementAt(biomes.keys.indexOf(it)))
}
}
return PointData(null, Savannah())
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Collection doesn't contain element at index -1.Ragvax
03/18/2021, 6:17 AMDenis Sazonov
03/18/2021, 10:42 AMJordan Foo
03/18/2021, 3:19 PMfun Bar.newFunction() {}
What exactly does this do?
fun Bar.() {}
tim
03/18/2021, 4:51 PMAccountState
object in one micro service. Now for the various use cases that act on this state all have narrowed attributes of the state they're interested in. For example, Login only cares about Username and Password, whereas Change Profile Details cares about the Name and Address fields.
When I retrieve data from mongo, I'd like to use a projection to only return the fields needed in the running use case and then I'd like to de-serialise that projection into something where my use cases can access those values without being cluttered with every filed that on the complete state object. ie. i don't want to create a version of the state where every value is nullable / wrapped in an Option.
In typescript I would use something along these lines:
type State {
const id: string
const username: string
const password: string
const name: string
const address: string[]
}
type NarrowViewOfState = Pick<State, "name", "address">
But as kotlin doesn't offer the same type flexibility; has anyone had success here?Petru
03/18/2021, 4:52 PMchristophsturm
03/18/2021, 5:43 PMSlackbot
03/19/2021, 12:53 AMtherealbluepandabear
03/19/2021, 1:13 AMscott
03/19/2021, 1:19 AMKamilH
03/19/2021, 10:19 AMKodein-DB
in the multiplatform library, however because of the fact that it depends on serialization 1.1.0
it forces me to upgrade kotlin version, because of this exception:
Your current Kotlin version is 1.4.21, while kotlinx.serialization core runtime 1.1.0 requires at least Kotlin 1.4.30-M1.
When I increase a version of Kotlin to 1.4.30
problem disappears and it’s not a problem to do so on library side, but when I’m building a new version of my lib including Kodein-DB
and trying to run the app that includes my library compiler forces me to also upgrade Kotlin version on the app side (because it’s also using 1.4.21
). Is there any possibility to overcome this problem? I wouldn’t like to force my clients to upgrade to Kotlin 1.4.30
Tyluur
03/19/2021, 2:57 PMPHondogo
03/19/2021, 6:32 PMtherealbluepandabear
03/20/2021, 3:18 AMclass LootBox(item: Any) {
private var loot = item
}
Instead of
class LootBox<T>(item: T) {
private var loot: T = item
}
Luiz Aguiar
03/20/2021, 11:46 AMLuiz Aguiar
03/20/2021, 11:46 AMGlen
03/20/2021, 1:29 PMLuiz Aguiar
03/20/2021, 1:39 PMGlen
03/20/2021, 1:52 PMRobert Jaros
03/20/2021, 2:07 PMCLOVIS
03/20/2021, 2:17 PMGlen
03/20/2021, 2:38 PMLuiz Aguiar
03/20/2021, 3:03 PM