Tuang
02/18/2020, 3:20 AMam
02/18/2020, 6:28 AMstudentList.forEach {
if(it.id ==student.id){
list.add(it.copy(isFailed = false))
}else{
list.add(it)
}
}
JP
02/23/2020, 3:01 AMval f: (String) -> Unit = { it -> println(it.length) }
val x = "hello".also(::f)
This prints out these error messages:
error: type inference failed: inline fun <T> T.also(block: (T) -> Unit): T
cannot be applied to
receiver: String arguments: (KProperty0<(String) -> Unit>)
val x = "hello".also(::f)
^
error: type mismatch: inferred type is KProperty0<(String) -> Unit> but (String) -> Unit was expected
val x = "hello".also(::f)
^
While
val x = "hello".also(::println)
would work, why would above not?
What is KProperty0<>
?JP
02/23/2020, 5:35 AMimport java.util.*
fun getList(): List<Int> {
val arrayList = arrayListOf(1, 5, 2)
Collections.sort(arrayList, object : Comparator<Int> {
override fun compare = {x: Int, y: Int -> y - x}
})
return arrayList
}
gave me error messages:
Object is not abstract and does not implement abstract member public abstract fun compare(p0: Int!, p1: Int!): Int defined in java.util.Comparator
'compare' overrides nothing
Expecting '('
Can someone elaborate these messages?
edit:
additionally when I tried different codes in the line starting with override fun compare
,
1.
override fun compare(x: Int, y: Int) {
return y - x
}
this did not work as well, producing error messages
Return type of 'compare' is not a subtype of the return type of the overridden member 'public abstract fun compare(p0: Int!, p1: Int!): Int defined in java.util.Comparator'
Type mismatch: inferred type is Int but Unit was expected
I don’t understand the error message. Why was the expected type Unit
, not Int
? Shouldn’t the compiler expect the return type as Int
?
BUT
2. with a slight modification,
override fun compare(x: Int, y: Int): Int {
return y - x
}
this worked.
3.
override fun compare(x: Int, y: Int) = y - x
This works also. But in this case, I also didn’t explicitly write the return type, same as approach 1.
What are the differences between them? Why did 1. produce error while the other two didn’t? What’s happening under the hood?tim
02/24/2020, 8:39 AMkeishi kubo
02/24/2020, 1:59 PMUsing higher-order functions imposes certain runtime penalties: each function is an object, and it captures a closure, i.e. those variables that are accessed in the body of the function. Memory allocations (both for function objects and classes) and virtual calls introduce runtime overhead.
But it appears that in many cases this kind of overhead can be eliminated by inlining the lambda expressions.
keishi kubo
02/24/2020, 2:26 PMvinny2020
02/24/2020, 8:40 PMvinny2020
02/24/2020, 8:40 PMEllen Spertus
02/24/2020, 11:49 PMSet<String>
containing all of the words in the file. Is there a way to map
or flatMap
over the lines of a file? I’ve gotten as far as bufferedReader().useLines()
.jk2018
02/26/2020, 11:36 PMNikita Khlebushkin
02/27/2020, 8:21 PMGilberto Diaz
02/28/2020, 12:39 PMvinny2020
02/28/2020, 9:08 PMbodiam
03/01/2020, 2:23 AMmyMap.mapWithIndex { (index, key, value ) -> .... }
available? I know it's possible with arrays, but I "need" to have an index counter (my current approach is just to have a counter var, but I was hoping there would be something more elegant)Chills
03/01/2020, 5:57 AMhooliooo
03/05/2020, 1:52 PMclass Test {
companion object {
const val A: Int = 0
const val B: Int = 1
const val C: Int = 2
const val DInt = 3
const val E: Int = 4
}
}
This companion object will only have constant valsbodiam
03/06/2020, 7:05 AMinterface MyInterface<out T> {
fun process(): T
}
I need to convert this interface to Java because of SAM conversion, but I'm failing a bit here.Kirill Prybylsky
03/06/2020, 8:44 AMLastExceed
03/06/2020, 1:55 PMassert()
from the kotlin stdlib and assertEquals()
from junit. the former is basically implemented as
if (!condition)
throw Exception(message)
its as simple and intuitive as can be. meanwhile the implementation of the latter goes so far down the rabbit hole (I have to navigate through 4 different files for what is actually just 5 lines of code) that I'd have trouble understanding what the function even does if it wasn't for the descriptive name. And this isn't just some random snippet from a stranger on the internet, its one of the most popular testing frameworks out there.
just why??
Edit: I don't just mean to vent, I mean this as an actual question. Why is this? There's gotta be a reason for itJorge Castillo
03/09/2020, 10:08 AMKenneth
03/10/2020, 8:29 AMTia Petts
03/11/2020, 12:51 AMhooliooo
03/11/2020, 11:07 AMlistOf(A, B, C)
I want a way to compare A to B, B to C and A to C so they are all evaluated against each otherChristian Sousa
03/12/2020, 11:54 AMBadecx
03/13/2020, 1:26 PMBadecx
03/13/2020, 4:37 PMJP
03/14/2020, 12:43 PMfun List<User>.aggregate(): List<User> {
val result = this
.groupingBy { it.login } // Grouping<User, String>
.aggregate { key: String, accumulator: User?, element: User, first: Boolean
->
if (first) element
else User(
accumulator!!.login,
accumulator!!.contributions + element.contributions
)
}
return result.values.toList()
}
result
’s type becomes Map<String, User?>
, while I was expecting it to be Map<String, User>
?chrisalbright
03/16/2020, 8:41 PMharry.singh
03/17/2020, 11:08 PMWhen you're using a smart cast with a property of a class, the property has to be a val and it can't have a custom accessor. Otherwise, it would not be possible to verify that every access to the property would return the same value.Makes sense but one thing that I don't understand is, what does the author mean
property has to be a val and it can't have a custom accessorDoesn't val properties provide only getter methods?
harry.singh
03/17/2020, 11:08 PMWhen you're using a smart cast with a property of a class, the property has to be a val and it can't have a custom accessor. Otherwise, it would not be possible to verify that every access to the property would return the same value.Makes sense but one thing that I don't understand is, what does the author mean
property has to be a val and it can't have a custom accessorDoesn't val properties provide only getter methods?
araqnid
03/17/2020, 11:24 PMval someProperty get() = …
= custom accessorharry.singh
03/18/2020, 12:08 AMval someProperty get() = ...
fails to verify that the val was changed?Quy D X Nguyen
03/18/2020, 12:15 AMaraqnid
03/18/2020, 12:21 AMval someProperty: String? get() = if (System.currentTimeMillis % 2 == 0) "hello" else null
- the nullness-or-not is unpredictableharry.singh
03/19/2020, 3:18 PM