marcinmoskala
11/21/2017, 2:00 PMsort
is sorting collection while sorted
is returning sorted copy of the collection.marcinmoskala
11/21/2017, 2:01 PMmarcinmoskala
11/21/2017, 2:04 PMmarcinmoskala
11/21/2017, 2:06 PMmarcinmoskala
11/21/2017, 2:18 PMmarcinmoskala
11/21/2017, 2:21 PMmarcinmoskala
11/21/2017, 2:31 PMfun bestStudents(allStudents: List<Student>) = allStudents
.sortedBy { it.grade }
.take(10)
marcinmoskala
11/21/2017, 2:34 PMfun bigger(a: Int, b: Int) = if (a > b)
a
else
b
fun greet(a: Any) = when (a) {
is String -> "Hello String"
is Number -> "Hello Number"
else -> "I don't know you! Go away!"
}
marcinmoskala
11/21/2017, 2:40 PMmarcinmoskala
11/21/2017, 2:46 PMmarcinmoskala
11/21/2017, 2:48 PMmarcinmoskala
11/21/2017, 2:51 PMtrimMargin
to not trim intentional indent:
val a = """if(a > 1) {
| return a
|}""".trimMargin()
oblakr24
12/18/2017, 9:12 AMdiesieben07
12/18/2017, 9:41 AMpardom
12/20/2017, 5:21 PMnic
01/09/2018, 11:22 PMlazy
property that can be cleared, and the next time you go to grab it, it creates a new instance of itself, so you don’t have to scatter the code with variable?.
everywhere, since you’ll never grab a null version of ittapchicoma
01/12/2018, 7:01 AMnullableValue = NullableValue(1, 2, 3)
nullableValue?.someProperty = 4
or 2️⃣ :
nullableValue = NullableValue(1, 2, 3).apply {
someProperty = 4
}
where NullableValue
constructor has a lot of params.voddan
01/12/2018, 10:51 PMNullableValue(.....)
a function or a constructor?robin
01/13/2018, 5:47 PMmy.root.package.subpackage
, the new file will get package subpackage
at the start, because of course the IDE doesn't know about my root package. Right now I got it to work by adding the root package manually inside the new file template in the settings, but that seems way too hacky for me. Is there a better approach to get the desired behavior?Dave Leeds
01/15/2018, 8:40 PM- Property declarations and initializer blocks
- Secondary constructors
- Method declarations
- Companion object
Do not sort the method declarations alphabetically or by visibility, and do not separate regular methods from extension methods. Instead, put related stuff together, so that someone reading the class from top to bottom would be able to follow the logic of what's happening.
I’m looking for thoughts on location of private extension properties. Would you tend to put them:
1️⃣ With the regular properties of the class
2️⃣ Near the functions that use them
3️⃣ Somewhere else (please specify)arekolek
01/17/2018, 2:58 PMuhe
01/18/2018, 9:54 AMval foo = mapOf<String, String>()[""] ?: longAssMethodNameWithLongAssParameters(lookAtMeImSoLong = "so very long!") inlineInfixFun { return }
becomes
val foo = mapOf<String, String>()[""]
?: longAssMethodNameWithLongAssParameters(lookAtMeImSoLong = "so very long!")
inlineInfixFun { return }
which doesn't compile any longerCzar
01/18/2018, 1:01 PMcompanion object
is suggested to be placed at the bottom of the class. This does not make sense to me. CO usually contains constants or stuff like : KLogging()
so why would anyone want to hide it at the bottom? These constants and logger
property in case of KLogging
IMO should be declared at the top so that they be easy to reference "at a glance".enleur
01/23/2018, 3:13 PMefemoney
01/25/2018, 7:03 AMefemoney
01/25/2018, 7:34 AMhelpermethod
01/25/2018, 9:39 AMmenegatti
01/26/2018, 2:31 PMlateinit
cannot be applied to primitive types such as menegatti
02/22/2018, 2:48 PMwaqas
02/23/2018, 11:43 AMinline fun <A, B, R> ifNotNull(a: A?, b: B?, code: (A, B) -> R) {
if (a != null && b != null) {
code(a, b)
}
}
...
fun test() {
ifNotNull(name, age) { name, age ->
doSth(name, age)
}
}
waqas
02/23/2018, 11:43 AMinline fun <A, B, R> ifNotNull(a: A?, b: B?, code: (A, B) -> R) {
if (a != null && b != null) {
code(a, b)
}
}
...
fun test() {
ifNotNull(name, age) { name, age ->
doSth(name, age)
}
}
bdawg.io
08/08/2018, 6:38 PMfun test() {
ignoreNpe {
doSth(requireNotNull(name), requireNotNull(age))
}
}