Slackbot
09/17/2022, 10:19 AMEllen Spertus
09/18/2022, 1:47 AMEric Womer
09/18/2022, 2:34 PMrrva
09/18/2022, 5:29 PMobject Foo { ... }
why is it still possible to read an instance of this "class" using jackson objectMapper with objectMapper.readValue(input, Foo::class.java)
and then I can access fields from that.. Does this not violate the singleton?homchom
09/18/2022, 9:43 PMPair<List<String>, String>
is just for testing purposes and is not final.)Midoriya
09/19/2022, 4:45 AMdata class TodoItem(
val taskName :String,
val isCompleted : Boolean = false,
)
private val taskList = mutableListOf<TodoItem>()
how can i change an element 'isCompleted' to true.
so if user said 4th element is completed how can i change it to complete
taskList[index] = TodoItem(taskName = taskList[index].taskName, isCompleted = true)
like this???cpe
09/19/2022, 8:05 AMcompanion object
. So I decided to provide an interface (or an annotation) and find the functions to call on startup as follows using (using classgraph as for scanning - I’m not bound to that framework):
interface OnStartup {
fun onStartup()
}
// a class that wil instantiated a lot of times during runtime
data class SomeClass(val name: String) {
companion object : OnStartup {
override fun onStartup() {
// notify someone at startup about our existence
}
}
}
fun main() {
ClassGraph().enableAllInfo().acceptPackages("com.test").scan()
.getClassesImplementing(OnStartup::class.java)
.filter { it.isStatic }
.flatMap { findStartupMethods(it) }
.forEach {
it.call()
}
}
private fun findStartupMethods(classInfo: ClassInfo): List<KFunction<*>> {
return classInfo.methodInfo.filter { function ->
function.name == "onStartup"
}.mapNotNull { method ->
method.loadClassAndGetMethod().kotlinFunction
}
}
This will not work as call()
expects a parameter: Callable expects 1 arguments, but 0 were provided.
which should be the instance of the class (from my understanding), so I guess it should be the companionObjectReference
. The code works well if I define the onStartup
function on top level (but that has negative impacts for my idea). Is there any way to retrieve the companionObjectReference
? Or to achieve this behaviour? Maybe there is another annotation scanner that fits better for Kotlin? Any help is appreciated.Michael de Kaste
09/19/2022, 8:27 AM.single()
, I would expect there to be function that does this:
• If there are no elements: null
• If there is one element: it
• If there are more elements: exception
However, the implementation .singleOrNull()
returns null on multiple elements too.
There have been several occassions where we now check size first and manually do a 0,1,else check to do this correctly, but am I just missing something or should I just do a language proposal?Christoph Frädrich
09/19/2022, 9:55 AMErik
09/19/2022, 10:02 AMList<T>
at some index, i.e. I'd create a new list.
Currently I do:
val list: List<T> = /*..*/
val elementToReplace: T = /*...*/
val newElement: T = /*..*/
val newList = list.toMutableList().apply {
this[indexOf(elementToReplace)] = newElement
}
E.g. as an extension:
fun <T> List<T>.replaceWith(old: T, new: T): List<T> = toMutableList().apply {
this[indexOf(old)] = new
}
Can this be done with some stdlib function, or in a shorter more idiomatic way?Gasan
09/19/2022, 11:52 AMJvmStatic
I want to write a junit/mockito test where I want to mock the method. Is there a way to do it? What if I remove JvmStatic
? What are my options?
I think it’s a part of the broader topic - how does the companion object relate to beans/singleton concepts of Spring
, for example.
Ideally, I would want no static methods at all. But instead having a singleton-y classes that I could mock or do whatever I can with the normal classes.CLOVIS
09/19/2022, 12:02 PMabstract class A {
abstract inner class B : A() {
init {
// How can I access the outer class?
}
}
}
In that example, this
refers to the class B
, this@A
refers to the parent class. How can I access the outer class?Jérémy CROS
09/19/2022, 3:29 PMfun smartCast(
condition1: String?,
condition2: String?
): String? {
if (condition1 == null || condition2 == null) return null
return dummy(condition1) // OK, smart cast succeed
}
fun smartCastFailed(
condition1: String?,
condition2: String?
): String? {
val checkCondition = condition1 == null || condition2 == null
if (checkCondition) return null
return dummy(condition1) // KO, smart cast fails
}
fun dummy(condition: String): String = TODO("do something with condition")
That might seem like a stupid example but I had a bunch of conditions that would not fit in one line and I also wanted to explicit a bit the check with the variable.
I assume this type of optimization is a bit hard for the compiler?
Any way around that? I guess using !!
is fine since I’m sure the condition is not null.Gasan
09/19/2022, 8:06 PMGasan
09/20/2022, 7:40 AMreactormonk
09/20/2022, 9:47 AMparseResult
demands a non-null return value - however, null
is a possible result I may get in there. How do I wrap the null
into a non-null value, so the compiler is happy with me?Gleb Minaev
09/20/2022, 11:50 AMabu naser
09/21/2022, 11:52 AMimport org.junit.Test
class TestMutableSetDelete {
@Test
fun testRemoveMutableSet(){
val data = TestSetList(mutableSetOf(), mutableSetOf())
add(data)
change(data)
//deletes not work in any inner value ever change
delete(data)
// removeIf always works . but i my code need to work below api 24 too.
// data.span.removeIf { it.start==it.end }
// FIXME: without change this works . but if change happend its not
data.range.forEach { println("start > ${it.start} > ${it.end}") }
assert(data.range.size==2)
}
}
data class TestSetList(
val range: MutableSet<Range> = mutableSetOf(),
val para: MutableSet<Para> = mutableSetOf()
)
data class Para(
val start:Int,
val end:Int
)
data class ChangedData(
var name:String="",
var age:Long
)
data class Range(
val start:Int,
val end:Int,
val changedData: ChangedData
)
fun add(data:TestSetList){
val cd =ChangedData("naser",26)
data.range.add(Range(0,0,cd))
data.range.add(Range(1,2,cd))
data.range.add(Range(1,5,cd))
data.range.add(Range(2,2,cd))
data.range.add(Range(3,3,cd))
}
fun change(data: TestSetList){
//simulate some change in the process .
data.range.forEach { it.changedData.name = it.start.toString() }
}
fun delete(data:TestSetList){
data.range.filter { it.start == it.end }.forEach { data.range.remove(it) }
}
mattinger
09/21/2022, 1:27 PMSOME_VALUE_WE_DONT_CARE_ABOUT -> { }
The issue is that sonarqube is treating this as a code smell, and our quality gate is blocking merges when this happens. So we have to either change our quality gate, or use admin privs to merge. Does anyone know if there’s a way to tell sonar to ignore these?Gasan
09/21/2022, 1:51 PMMárk Bartos
09/22/2022, 8:52 AMkotlin
InputDecoration(color=if(isValid) blue else red) {
Input(value, onChange)
}
Joshua Hansen
09/24/2022, 5:45 AMval foo: Map<String, String> = if (str.contains(bar1)) {
map1
} else if (str.contains(bar2)) {
map2
} else if (str.contains(bar3)) {
map3
} else {
defaultMap
}
The maps are properties defined in the class that this function is in. There is potential for more maps to be added in the future. This is basically a way for me to select which map to use. str
is a value passed into this function and is expected to meet 1 of the 3 criteria.
Is there a more clever way to do this?zt
09/25/2022, 5:09 AMrandom.choices()
method which lets you provide a list of options and a list of weights for them. Is there some equivalent way to do that in kotlin?D Tam
09/25/2022, 12:05 PMYusuf.I
09/25/2022, 7:12 PMinterface ClientEvents {
ready: () => void;
}
export declare interface Client {
on<Event extends keyof ClientEvents>(
event: Event,
listener: ClientEvents[Event],
): this;
off<Event extends keyof ClientEvents>(
event: Event,
listener: ClientEvents[Event]
): this;
emit<Event extends keyof ClientEvents>(
event: Event,
...args: Parameters<ClientEvents[Event]>
): boolean;
}
Ellen Spertus
09/25/2022, 11:06 PMString
API and have a few questions... https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/zt
09/26/2022, 2:32 AMCLOVIS
09/26/2022, 8:53 AMnull
is a valid value for out Any?
, right?CLOVIS
09/26/2022, 9:18 AMinterface Param
data class A<P : Param?>(val v: (P) -> Unit)
fun <P> create(v: (P) -> Unit) = A(v)
fun main() {
val a = create { _: Nothing? -> }
println(a.v(null)) // Null can not be a value of a non-null type Nothing
}
According to IDEA, a
has type A<out Any?>
, so why is null
not allowed? (playground link)martmists
09/26/2022, 6:12 PM\xFF
escapes in strings? I can only find \uFFFF
but this will likely create two bytes instead of one