ursus
07/09/2022, 9:24 AMAdam S
07/10/2022, 6:47 AMMutableSharedFlow<String>
, and then another SharedFlow<String>
that in another coroutine awaits new messages from the file-store and forwards them to the Kafka topic
(I'm using Kotlin/JVM 1.7, Ktor 2.0, Kakfa 3.2)Chris Cordero
07/10/2022, 11:05 PMimport com.fasterxml.jackson.core.type.TypeReference
import java.util.function.Supplier
class Bar(val x: Int)
fun foo(whatever: Supplier<TypeReference<out Bar>>) {}
fun main() {
val typeRef = object : TypeReference<Bar>() {}
val supplier = Supplier<TypeReference<Bar>> { typeRef }
foo(supplier) // expected Supplier<TypeReference<out Bar>>, got Supplier<TypeReference<Bar>>
}
Chris Cordero
07/10/2022, 11:06 PMimport java.util.function.Supplier
class Bar(val x: Int)
fun foo(whatever: Supplier<out Bar>) {}
fun main() {
val supplier = Supplier<Bar> { Bar(42) }
foo(supplier) // compiler happy
}
Manuel Dossinger
07/11/2022, 7:35 AMthana
07/11/2022, 8:45 AMprivate val mutable = mutableListOf<Something>()
val readonly: List<Something> = mutable
which is kinda uglyLandry Norris
07/11/2022, 4:14 PMfun assertWithin(expected: Double, actual: Double, tolerance: Double) {
val message = "Difference of ${actual-expected} is out of range. " +
"Expected $expected, got $actual"
if(abs(expected-actual) > tolerance) println(message)
assertTrue(abs(expected-actual) <= tolerance, message)
}
I see this message in the console, though
Difference of -6.004799503160669E14 is out of range. Expected 0.75, got -6.004799503160661E14
How could (-6.004799503160661E14)-(0.75) be equal to (-6.004799503160661E14)?
Edit: misread the exponent. subtracting 0.75 from 6*10^14 is essentially 6*10^14reactormonk
07/11/2022, 4:16 PMColton Idle
07/12/2022, 5:11 AMLoney Chou
07/12/2022, 5:57 AMfun calculate(): Int
And inside it I need to call a suspend function. How to do that? runBlocking
?nkiesel
07/12/2022, 6:58 AMfun <K, V> Map<K, V>.reverse(): Map<V, List<K>> = entries.groupBy { it.value }.map { it.key to it.value.map { it.key } }.toMap()
reactormonk
07/12/2022, 12:24 PMinspect
? Where you get a full dump of a class, not just the toString
Ciox
07/12/2022, 1:41 PMLandry Norris
07/12/2022, 7:30 PMStylianos Gakis
07/13/2022, 9:07 AM* | name | required | description | default value | example |
* | :--- | :--- | :--- | :--- | :--- |
* | name | ✅ | desc | ❌ | example |
It renders as I would expect, with the items left-aligned and everything, but everything is way too close to each other. Is there a way in this formatter to make it look like an actual table? Preferably keep some dividers vertically and horizontally across the items, but if not at least some way to force more space between the items.reactormonk
07/13/2022, 11:30 AMsrc/main/java
, I get https://stackoverflow.com/questions/59366460/org-jetbrains-kotlin-resolve-lazy-nodescriptorfordeclarationexception-descriptoTies
07/13/2022, 3:10 PM@JvmInline
value class Foo(val value : Int) {
}
//This doesnt work
fun bar(myFoo : Foo) : Int{
return myFoo + 1
}
//This works
fun bar(myFoo : Foo) : Int{
return myFoo.value + 1
}
reactormonk
07/14/2022, 8:09 AMTLV<T>
signature, and just use TLV
to represent it, if I'm using the interface to designate the type?
sealed interface TLV<T> {
val value: T
val type: Byte
}
object TLVNULL: TLV<Unit> {
override val type: Byte = 0x00
override val value: Unit = Unit
}
data class TLVNDEF(override val value: NdefMessage): TLV<NdefMessage> {
override val type: Byte = 0x03
}
data class TLVProp(override val value: ByteArray): TLV<ByteArray> {
override val type: Byte = 0xFD.toByte()
}
object TLVTerminator: TLV<Unit> {
override val type: Byte = 0xFE.toByte()
override val value: Unit = Unit
}
Colton Idle
07/14/2022, 7:18 PMenum class Foo(val key: String, val userName: String) {
Bar("bar_item", "Bar For You"),
Baz("baz_item", "Baz For Me"),
OTHER("other", "Other"),
}
fun getTheFoo(key: String): Foo {
return when (key) {
"bar_item" -> Foo.Bar
"baz_item" -> Foo.Baz
"other" -> Foo.OTHER
else -> Foo.OTHER
}
}
Matthieu Stombellini
07/14/2022, 8:37 PMprovideDelegate
to work with generic output types. Basically, what I'm trying to do is the following setup:
class Producer {
inline fun <reified T> create(): ReadOnlyProperty<Any, T> {
println("Creating a property with create(), return type is ${T::class}")
TODO()
}
inline operator fun <reified T> provideDelegate(thisRef: Any, prop: KProperty<*>): ReadOnlyProperty<Any, T> {
println("Creating a property with provideDelegate, return type is ${T::class}")
TODO()
}
}
class MyClass(producer: Producer) {
val one: String by producer.create()
//val two: String by producer
}
While the val one
works perfectly fine, if I uncomment val two
, it no longer compiles. Is there any way to do this? Kotlin Playground link: https://pl.kotl.in/vXOAoeauS
Compilation fails with:
Property delegate must have a 'provideDelegate(MyClass, KProperty<*>)' method. None of the following functions is suitable: public final inline operator fun <reified T> provideDelegate(thisRef: Any, prop: KProperty<*>): ReadOnlyProperty<Any, ???> defined in Producer
frogger
07/15/2022, 7:57 AMLukasz Kalnik
07/15/2022, 8:42 AMif
?
val selectedScene1: String? = // ...
val selectedScene2: String? = // ...
if (selectedScene1 != null || selectedScene2 != null) {
val sceneToFind: String = selectedScene1 ?: selectedScene2!! // The !! are necessary to make this work
}
João Gabriel Zó
07/15/2022, 4:58 PMmartmists
07/17/2022, 12:33 AMimport io.github.gunpowder.api.config
doesn't resolve.Lukasz Kalnik
07/18/2022, 9:33 AMdata class
toString()
output?
I have a lot of nested data classes and when a test fails it's quite cumbersome to compare the linear outputs of the standard toString()
implementation of a data class.Lukasz Kalnik
07/18/2022, 3:02 PMmapNotNull { if (it == removeMe) null else it }
doesn't support lists with nullable elements.
In my case the elements have unique IDs, so I could also filter {}
or dropWhile {}
based on ID, but the names are not obviously suggesting that only one element gets removed.
Do we have to do toMutableList().remove(removeMe).toList()
?Jiri Bruchanov
07/19/2022, 3:35 PMdimsuz
07/19/2022, 4:30 PMif (true) 1 else if (false) { 2 } else { 3 }.also { println("hello") }
if (true) 1 else { 3 }.also { println("world") }
print only "world" and not "hello"?
first one treats whole if/else as an expression why the latter one treats only else branch as an expression?Nat Strangerweather
07/19/2022, 6:06 PM@Composable
fun ShowDropDown(
openDropDown: MutableState<Boolean>,
text: List<String>,
destination: List<DirectionDestination>,
navigator: DestinationsNavigator,
) {
DropdownMenu(
expanded = openDropDown.value,
onDismissRequest = { openDropDown.value = false },
) {
text.zip(destination)
.forEach {
DropdownMenuItem(onClick = { navigator.navigate(it.second) },
text = { Text(it.first) })
}
}
}
Zinedine Bedrani
07/19/2022, 6:58 PMdata class Point(val x:Int, val y:Int)
val p = Point(5,13)
val getter1 = Point::x
val getter2: (Point) -> Int = Point::x
println(getter1(p)) // 5
println(getter2(p)) // 5
What I don’t understand is why with getter1 I was able to get reflection information about x for example I could do something like :
println(getter1.visibility) // PUBLIC
println(getter1.returnType) // <http://kotlin.Int|kotlin.Int>
while with getter2 I can’t, even if I use the @OptIn(ExperimentalReflectionOnLambdas::class)
with println(getter2.reflect()?.returnType)
the result is null.Zinedine Bedrani
07/19/2022, 6:58 PMdata class Point(val x:Int, val y:Int)
val p = Point(5,13)
val getter1 = Point::x
val getter2: (Point) -> Int = Point::x
println(getter1(p)) // 5
println(getter2(p)) // 5
What I don’t understand is why with getter1 I was able to get reflection information about x for example I could do something like :
println(getter1.visibility) // PUBLIC
println(getter1.returnType) // <http://kotlin.Int|kotlin.Int>
while with getter2 I can’t, even if I use the @OptIn(ExperimentalReflectionOnLambdas::class)
with println(getter2.reflect()?.returnType)
the result is null.Joffrey
07/19/2022, 7:03 PMgetter1
and getter2
are not the same. By specifying a wider type on getter2
, you're losing the specificities of the more precise type of Point::x
(KProperty1<Point, Int>
). Using the actual KProperty
type comes with extra information.
A simple analogy would be val getter3: Any = Point::x
- do you expect to be able to access visibility
and returnType
here?.reflect()
on the getter2
function will just give you a KFunction
, not really information about the actual property x
Zinedine Bedrani
07/19/2022, 7:08 PM(Point) -> Int
is a function type, so r`eflect()` should return KFunction
, isn’t it ? so I don’t understand why reflect() doesn’t return KProperty.Getter
in this case, or Am I wrong ?