dave08
12/08/2021, 12:26 PM@ExperimentalStdlibApi
inline operator fun <reified T : Any> SharedPreferences.getValue(owner: Any?, property: KProperty<*>): T {
return when (val type = typeOf<T>()) {
is Set<*> ->
getStringSet(property.name.replace('_', '.'), emptySet()) ?: emptySet()
else -> error("Invalid type")
} as T
}
dave08
12/08/2021, 5:01 PMclass Foo { fun get(): Something }
operator fun Bar.getValue(o: Any?, property: KProperty<*>): Something = Foo().get()
val prop by Bar()
Will the Foo instance get GCed?neworldlt
12/08/2021, 5:25 PMJasin Colegrove
12/09/2021, 11:42 AMd-static
12/09/2021, 2:59 PMAlex
12/09/2021, 5:59 PMval x:DoubleArray = doubleArrayOf(0.0, 0.19, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25)
the values are records every 1.5s distance travelled, in this case 0.0 is the starting point 0.19 is when the distance covered is recorded at 1.5 seconds mark after another 1.5 seconds the distance of 0.5 is recorded and so on.
2. I need to find the difference in distances as following 0.0-0.19, 0.19-0.5, 0.5-0.75, 0.75-1.0, 1.0-1.25, 1.25-1.50, 1.5-1.75, 1.75-2.0, 2.0-2.25
, i know how to use map(to double values or multiple then) but i don't know how to subtract from a preceded value with a previous value in an Array
3. you can refer to the full question if my explanation is not clear, i just need to find the difference in distances before i can proceed to do the next step, hope someone can help me on this 😅bbaldino
12/10/2021, 10:18 PMclass Foo {
public static Foo.Builder Builder() { ... }
public static class Builder {
public Builder() { ... }
}
}
and i’m trying to call it from kotlin like:
Foo.Builder()
...
but i get complaints about ambiguity…i think because the helper type in Foo
matches the class name (and therefore the constructor) exactly. does anyone know if there’s a way to disambiguate between the two from kotlin?martmists
12/11/2021, 1:52 AMDraget
12/12/2021, 1:26 AMprivate data class MyClass(val intVal: Int) {
constructor(stringVal: String) : this(someFunction(stringVal))
}
fun someFunction(stringVal: String): Int {
println("I can run some random code here")
val foo = stringVal.reversed()
return foo.toInt()
}
But how could I run code on the secondary constructor without using a function?Camshaft54
12/12/2021, 4:54 PM// getVal() returns a nullable value
fun example() {
val myVal = getVal() ?: {
println("value is null")
return
}
}
Colton Idle
12/12/2021, 9:15 PMclass StringUtil {
fun getInitialsFromName(name: String): String {
or just create
class InitialGeneratorService {
fun get(name: String): String {
Or should it be a companion or just make a string extension method (seems easily abused though)
I'm definitely overthinking it, but approciate any advicedave08
12/13/2021, 2:47 PMinterface Foo
class Foo1 : Foo
interface Bar {
fun <T : Foo> baz(foo: T)
}
// This doesn't work, why? It IS derived from Foo... is there any way to do such a thing w/o taking in a Foo and casting it to Foo1?
class Bar2 {
override fun baz(foo: Foo1) = TODO()
}
Miguel
12/13/2021, 4:05 PMprivate suspend fun suspendFunction() : Boolean { ... }
launch {
val returnedValue = suspendFunction()
if (returnedValue) anotherFunction() // this does not fire in the debugger
}
What am I doing wrong?Matic
12/13/2021, 8:12 PMfindNavController(R.id.nav_host_fragment_content_main).navigate(R.id.settingsFragment)
I'm using this inside onOptionsItemSelected in the activity (I am trying to a settings fragment when I click on the button in ActionBar).
Although this is working, if I click on the settings 5 times, I also need to click back 5 times.
I am aware this is intended behaviour, but I can't seem to find how to avoid this.nkiesel
12/13/2021, 11:09 PM$
in identifiers (e.g. A$B
is a valid identifier). Kotlin allows quoted identifiers like ``A$B`` . But https://kotlinlang.org/spec/syntax-and-grammar.html#identifiers states that letters are "<any unicode character of classes LL, LM, LO, LT, LU or NL>" and that identifiers are "(Letter | '_'
) {Letter | '_'
| UnicodeDigit} | `'`'` QuotedSymbol {QuotedSymbol} `'`'` ". Does that not mean that e.g. Ⅲ (aka \u2162) - which is part of unicode character class NL - should be a valid identifier? I just tried but the compiler does not like val Ⅲ = "roman 3"
.mcpiroman
12/14/2021, 10:36 AMclass Foo
class Bar
Why I can't do `Foo() == Bar()`but can Foo().equals(Bar())
?martmists
12/14/2021, 11:45 PMfun <R> convertTo(): R {
return convertImpl()
}
fun convertImpl(): Int {
// ...
}
fun convertImpl(): String {
// ...
}
Is there an easy way to do it or will I have to just use a when statement and make R reified and add a branch for each implemented type?Ayfri
12/15/2021, 9:30 AMdata class Zone(x: IntRange, y: IntRange, z: IntRange)
and
data class Zone(x1: Int, y1: Int, z1: Int, x2: Int, y2: Int, z2: Int)
?
And would it impact performances to use Ranges ?
I only need like 50 zones maximum for my projectAn Tran
12/15/2021, 5:25 PMColton Idle
12/16/2021, 9:57 AMPhilipp Dargel
12/16/2021, 2:51 PMassert("asdf") {
hasSize { it isEq 4 }
}
Pablo Schmid
12/16/2021, 7:50 PMabstract class MyClass<T>(){
abstract fun blocking(): T
fun <R> execute(run: (T)->R, onError: ((e: Throwable)->R)? = null): R {
return runCatching { run(blocking()) }.getOrElse{ e -> onError?.let { it(e) } ?: throw e}
}
}
internal class MyClassImpl(): MyClass<String>() {
override fun blocking(): String {
return "something"
}
}
I would like to mock the execute function in MyClassImpl that extends MyClass
I usually mock functions as follows but it's not compiling as it can't infer the types
private val myVariable = mock<MyClassImpl>().also {
whenever(it.execute(any(), anyOrNull()).thenReturn(any())
}
Really appreciate the help.Florian
12/17/2021, 10:13 AMOrhan Tozan
12/17/2021, 3:31 PMjava.lang.IllegalStateException: Symbol for kotlin.collections/mutableMapOf|-4813910536206556932[0] is unbound
at org.jetbrains.kotlin.ir.symbols.impl.IrBindablePublicSymbolBase.getOwner(IrPublicSymbolBase.kt:52)
Miguel Vargas
12/17/2021, 10:33 PMfun assertListTypes(list: List<ParentType>, types: List<KType>) {
assertEquals(list.size, types.size)
types.forEachIndexed { idx, type ->
assert(list[idx] is type)
}
}
assertListTypes(list, listOf(typeOf<Class1>(), typeOf<Class2>()))
But the compiler didn’t like the is
check on the KType. Any ideas?Jonathan Briceño
12/20/2021, 9:26 PMArnab
12/21/2021, 11:53 AMelse
branch when I have covered all of the possible subclasses of Foo
(sealed class) ? Was it always like this? I remember not having to implement an else
branch before.Vitali Plagov
12/21/2021, 1:13 PMapply{}
function inside. Like:
fun Foo.myFun() {
bar.apply {
this.doA()
this.doB()
this.doC()
}
}
this
here refers to the type of bar
, not to the type of Foo
How can I use the instance of Foo
inside the bar.apply{}
?Nikolay Lebedev
12/21/2021, 2:35 PMMutableList
as it's property.
I want to iterate through this list and change values of elements.
So I use listIterator
and its set
method to change element values.
But when I exit a loop and print out a list from original object I see no changes.
How do I change the original object?dave08
12/22/2021, 12:15 PMsealed interface Foo<R>
object NoFoo : Foo<Nothing>
data class SomeFoo<R> : Foo<R>
// Is there any way this could work? (It doesn't... even though I don't use the R in NoFoo)
val baz: Foo<Baz> = NoFoo
dave08
12/22/2021, 12:15 PMsealed interface Foo<R>
object NoFoo : Foo<Nothing>
data class SomeFoo<R> : Foo<R>
// Is there any way this could work? (It doesn't... even though I don't use the R in NoFoo)
val baz: Foo<Baz> = NoFoo
Rob Elliot
12/22/2021, 12:56 PM<out R>
sealed interface Foo<out R>
object NoFoo : Foo<Nothing>
data class SomeFoo<out R>(val r: R) : Foo<R>
interface Baz
val baz: Foo<Baz> = NoFoo
dave08
12/22/2021, 1:09 PMin
with Any
instead of Nothing
...Rob Elliot
12/22/2021, 1:40 PMin Any
sounds like a pretty useless type.dave08
12/22/2021, 1:43 PM() -> Foo<SomeType>
then, if I have a SomeFoo
as a result of that callback, then I need it in something like setResult(someFoo.value)
Rob Elliot
12/22/2021, 1:56 PMsealed interface Foo<out R> {
val value: R
}
object NoFoo : Foo<Nothing> {
override val value: Nothing
get() = error("unsupported")
}
data class SomeFoo<out R>(override val value: R) : Foo<R>
val foo: Foo<String> = TODO()
if (foo is SomeFoo) {
setResult(foo.value)
}
kotlin.Result
?dave08
12/22/2021, 5:01 PMkotlin.Result
only has the last two...<Any>
there is so bad... compared to the alternatives that are much more verbose...?