miqbaldc
08/17/2019, 9:07 AMSlackbot
08/25/2019, 2:29 PMkarelpeeters
09/04/2019, 9:02 PMi
itself just do repeat(max) { ... }
Cody Engel
09/04/2019, 9:09 PMprivate inline fun <reified T : Any> T.`💩`(fieldName: String, fieldValue: Any) {
val attribute = this::class.java.getDeclaredField(fieldName)
attribute.isAccessible = true
attribute.set(this, fieldValue)
}
// call site
agent.`💩`("id", agentId)
Milan Hruban
09/17/2019, 8:09 AMPrefer using the expression form of try, if and when.
Should I apply this with no exceptions? e.g.
fun doSomething(x: Int): Int {
if (x > 10) {
<http://log.info|log.info>("an info")
someAction()
return 5
} else {
log.error("an error")
return 10
}
}
Should this be converted to return the if
expression?Dmitry Kandalov
09/18/2019, 9:14 AMmiqbaldc
10/05/2019, 2:31 PM// utils/Const.kt
const val DEFAULT_TIME_MILLIS = 3000L // for default timer in ms
const val DEFAULT_PAGER_SIZE = 3 // for viewpager ui const
Need an advice
Does we should avoid building a single god class constant like this? Or what approach highly recommended to create a constant class?Felipe Álvarez
10/09/2019, 3:29 PMprivate static final
attributes to avoid memory footprint and object creation. In Kotlin, I created a companion object with a private const val
but when decompiled code shows a new inner static class is created, does this create more memory footprint than the Java counterpart? Is there a better way to define constant which should be used only in one class?Florian
10/28/2019, 8:27 AMdiego-gomez-olvera
10/31/2019, 1:42 PM==
vs ?:
in order to operate with Boolean?
. I generally try to avoid @Nullable
types, but sometimes they are inevitable.
I find more ‘natural’
value == false
Over
value ?: false
It seems more explicit and also easier to understand by developers new to Kotlin. Do you have any recommendations?kz
11/11/2019, 9:38 PMHullaballoonatic
11/29/2019, 6:07 PMFoo(bar.prop.op1 {...}.op2 {...}.op3(...))
or
bar.prop.op1 {...}.op2 {...}.op3(...).let(::Foo)
?Hullaballoonatic
12/01/2019, 1:35 AMfun Vector.times(other: Vector) = zip(other, Double::times).toVector()
or
fun Vector.times(other: Vector) = zip(other) { a, b -> a * b }.toVector()
?Hullaballoonatic
12/04/2019, 6:59 PMif (cond) foo(a) else foo(b)
or
foo(if (cond) a else b)
gabrielfv
12/09/2019, 4:16 PMinline fun <reified T : Any> withType(
t: KClass<T> = T::class
) = ...
inline fun <reified T1 : Any, reified T2 : Any> withType(
t1: KClass<T1> = T1::class,
t2: KClass<T2> = T2::class
) = ...
inline fun <reified T1 : Any, reified T2 : Any, reified T3 : Any> withType(
t1: KClass<T1> = T1::class,
t2: KClass<T2> = T2::class,
t3: KClass<T3> = T3::class
) = ...
inline fun <reified T1 : Any, reified T2 : Any, reified T3 : Any, reified T4 : Any> withType(
t1: KClass<T1> = T1::class,
t2: KClass<T2> = T2::class,
t3: KClass<T3> = T3::class,
t4: KClass<T4> = T4::class
) = ...
Florian
12/11/2019, 9:30 AMFlorian
12/12/2019, 9:51 AMFlorian
12/13/2019, 10:06 AMinit
block or directly on the property?
val name: String = if (name.isBlank()) "No Name" else name.trim()
vs
init {
if (name.isBlank()) {
this.name = "No Name"
} else {
this.name = name.trim()
}
}
Hullaballoonatic
12/21/2019, 5:10 PMopen
class as opposed to abstract class
, sealed class
, or interface
?Florian
12/29/2019, 8:45 AMzak.taccardi
01/09/2020, 4:33 PMtypealias
naming - which style do you prefer (verb vs noun vs mixed)?
() -> User
// 1 - verb
typealias GetUser = () -> User
Usage then becomes getUser()
// 2 - noun
typealias UserGetter = () -> Int
Usage then becomes `userGetter()`/`userGetter.invoke()`
// 3 - mixed
typealias UserGetter = () -> User
val userGetter: UserGetter = ..
val viewModel = ViewModel(
getUser = userGetter
)
Usage is still getUser()
jordigarcl
01/11/2020, 11:03 AMfun createList(source: Source) = mutableListOf<Item>().also {
// Code that accesses source, adds item to list, etc
// On the long-ish side
}
ghedeon
01/14/2020, 4:25 PM@SerialName
for all fields or just the required ones?
1.
data class Foo(@SerialName("_id") val id: Int, @SerialName("name") val name: String)
2.
data class Foo(@SerialName("_id") val id: Int, val name: String)
Luke Sleeman
01/20/2020, 4:55 AM// 1
something?.let { return it }
Or
// 2
if(something != null) { return something }
gabrielfv
01/24/2020, 6:29 PMinterface StateMachine<S> {
fun updateState(state: S)
}
Where S
needs to be a class that is able to represent the state of an UI element. What would you set as the upper-bound of S
?
// 1
interface StateMachine<S : Any>
or
// 2
interface StateMachine<S : State>
// where
interface State // There is no specific contract expected
Ellen Spertus
01/28/2020, 12:06 AMprivate fun chooseBestMatch(results: List<String>) {
val lowers = results.map { it.toLowerCase() }
val matches: List<IntentMatcherResult> = lowers.map { result -> matchers.map { it.matchTranscript(result) } }.flatten().flatten()
val best = matches.maxBy { it.score }
}
Ellen Spertus
01/28/2020, 1:26 AM// 1
val h = hour.toInt() + if (meridian.isNotEmpty() && meridian[0] == 'p') 12 else 0
or
//2
var h = hour.toInt()
if (meridian.isNotEmpty() && meridian[0] == 'p') h += 12
Ellen Spertus
01/30/2020, 5:27 PM// 1
parameterRegex.matchEntire(toParse)?.run {
require(groupValues.size == 3)
val (parameter, value, rest) = destructured
parameters[parameter] = value
return parse(rest)
}
or
// 2
parameterRegex.matchEntire(toParse)?.let {
require(it.groupValues.size == 3)
val (parameter, value, rest) = it.destructured
parameters[parameter] = value
return parse(rest)
}
Ellen Spertus
01/30/2020, 5:55 PM// 1
regexBuilder.append(
alts.split("|")
.map { it.trim() }
.map { if (it.isEmpty()) "" else " $it"}
.joinToString(prefix = "(?:", separator = "|", postfix = ")"))
or
// 2
regexBuilder.append(
alts.split("|")
.map { it.trim() }.joinToString(
prefix = "(?:",
separator = "|",
postfix = ")"
) { if (it.isEmpty()) "" else " $it" })
Ellen Spertus
02/02/2020, 8:25 PM// 1
var c = 0 // count of number of times this substitution is made in text
val newText = text.replace(re) {
c += 1
sub
}
or
// 2
val c = re.findAll(text).asSequence().count()
val newText = text.replace(re, sub)
or something better?Ellen Spertus
02/02/2020, 8:25 PM// 1
var c = 0 // count of number of times this substitution is made in text
val newText = text.replace(re) {
c += 1
sub
}
or
// 2
val c = re.findAll(text).asSequence().count()
val newText = text.replace(re, sub)
or something better?Czar
02/02/2020, 9:11 PMval text = "abcdbdecbcd"
val matcher = "b".toPattern().matcher(text)
val sub = "Z"
var counter = 0
val newText = if(matcher.find()) {
buildString(text.length) {
do {
counter++
matcher.appendReplacement(this, sub)
} while (matcher.find())
matcher.appendTail(this)
}
} else {
text
}
// test:
check(counter == 3)
check(newText == "aZcdZdecZcd")
Ellen Spertus
02/02/2020, 9:12 PMStringBuilder
with StringBuffer
.Czar
02/02/2020, 9:15 PMStringBuilder
is better choice in this case.Ellen Spertus
02/02/2020, 9:16 PMCzar
02/02/2020, 9:16 PMEllen Spertus
02/02/2020, 9:16 PMCzar
02/02/2020, 9:17 PMEllen Spertus
02/02/2020, 9:17 PMCzar
02/02/2020, 9:19 PMEllen Spertus
02/02/2020, 9:19 PMCzar
02/02/2020, 9:20 PMre
to be a regex, or is it just a normal substring replacement?Ellen Spertus
02/02/2020, 9:21 PMc += 1
in my original code (transliterated from JS) rubbed me the wrong way because functional programming, but there’s no need to pretend I’m in a purely functional language.\\b(foo|bar|baz)\\b
).Czar
02/02/2020, 9:23 PMcounter++
, but won't be as readableEllen Spertus
02/02/2020, 9:23 PMCzar
02/02/2020, 9:24 PMEllen Spertus
02/02/2020, 9:24 PM2
.Czar
02/02/2020, 9:34 PMEllen Spertus
02/02/2020, 9:35 PMCzar
02/02/2020, 9:35 PMval text = "abcdbdecbdz"
val matcher = "(b)".toPattern().matcher(text)
val sub = "Z"
val count = matcher.results().count()
val newText = matcher.replaceAll(sub)
it's much more readable and understandable than the loop and counter var version.
The only drawback is that search is performed twice, as `replaceAll`resets the matcher state.Ellen Spertus
02/02/2020, 9:44 PMCzar
02/02/2020, 9:50 PMEllen Spertus
02/02/2020, 9:53 PMUnresolved reference: results
on the second-to-last line.Czar
02/05/2020, 6:47 PMresults()
was added in JDK9 I believeEllen Spertus
02/05/2020, 6:53 PMCzar
02/05/2020, 6:54 PM