Jason Ankers
02/19/2021, 11:48 AMForce
from Double
which represents a force value converted to the appropriate unit (lbs/kg) based on user preferences. The type would encapsulate the conversion logicescodro
02/19/2021, 1:25 PMinternal inline class CategoryId(val value: Long?)
fun test(){
CategoryId(12)
}
Warning: Use Long.valueOf(12) instead
user
02/19/2021, 5:09 PMManuel Pérez Alcolea
02/20/2021, 4:49 AMclass Asdf private constructor() {
companion object {
operator fun invoke(s: Asdf.() -> Unit) {
this.s() // doesn't make sense, Asdf().s() would work instead
}
}
}
// to be called as follows:
Asdf { }
this doesn't work because there's no this
in a companion object. I guess I could have a lazy property to an Asdf
instance, but can I have no instance at all?Gabriel
02/20/2021, 11:21 AMval avatarList = mutableListOf<Int>(1,2,3)
fun generateRandomAvatar(): Int? {
val pick = avatarList.randomOrNull()
if (pick !== null) avatarList.remove(pick)
return pick
}
Gabriel
02/20/2021, 12:02 PMfun addPlayer(
name: String,
avatar: Int? = generateRandomAvatar(),
id: String = UUID.randomUUID().toString()
) { dostuff }
...
fun PlayerList(players: List<Player>, addPlayer: (String, Int?, String) -> Unit) {
addPlayer("Player #${players.size}")<----error here
}
But I'm not sure how to convey in the signature that they're optional, maybe this isn't possible?Mjahangiry75
02/20/2021, 7:39 PMcoroutine channel
in a viewmodel
, how can I collect changes in multiple places?
I tried code below:
#1
channel.consumeAsFlow {}
#2
channel.consumeAsFlow {}
#3
channel.consumeAsFlow {}
but only the first one will get triggeredSlackbot
02/21/2021, 1:07 PMfrank
02/21/2021, 1:55 PM# Method 1:
private const val API_PATH = "<http://localhost:8080>"
const val WELCOME_ENDPOINT = "$API_PATH/welcome"
# Method 2:
object EndPoints {
private const val API_PATH = "<http://localhost:8080>"
const val WELCOME = API_PATH + "/welcome"
}
Marc
02/21/2021, 6:54 PMsealed class Result<out R> {
data class Success<out T>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
}
Mark
02/21/2021, 7:24 PMcompileClasspath
abd runtimeClasspath
, is it possible to disable this and have it only as testImplementation
?Adam
02/22/2021, 1:14 AMGleb Garipov
02/22/2021, 9:55 AMsealed class WriteOperation {
data class Insert(...some props): WriteOperation()
data class Delete(...some props): WriteOperation()
data class Replace... and so on
}
// And then in mongo-related package I do:
fun WriteOperation.toMongoModel(): WriteModel = when (this) {
is WriteOperation.Insert -> InsertOneModel(...initialization)
is WriteOperation.Delete -> DeleteOneOperation(...initialization)
is WriteOperation.Replace -> ...
... and also around 12 other conditions
}
// Same thing happens with SQL:
fun WriteOperation.toSqlQuery(): SQLQuery = when (this) {
...long and painful list
}
Is there any way of replacing those long when expressions with some kind of inheritance, extension or composition without referencing Mongo or SQL implementation in sealed classes?Animesh Sahu
02/22/2021, 12:44 PMtony
02/22/2021, 11:05 PM<https://dl.bintray.com/kotlin/kotlin-eap>
for accessing preview versions of Kotlin. What repo can I use instead, to remove my reliance on anything bintray/jcenter-related?scottiedog45
02/23/2021, 12:26 AMval cannot be reassigned
error, i think because i don't want to assign the local value in the forEach closure, but the property of the parent class. How could I do something like this?
listOf<Job?>(
uiJob,
authJob,
videoEventsJob,
videoJob,
countdownJob
).forEach {
it?.cancel()
it = null
}
user
02/23/2021, 11:53 AMmending3
02/24/2021, 4:50 AMval string = "<p></p>this<p></p>"
val arrs = listOf("<p data-id=\"1\"></p>", "<p data-id=\"2\"></p>")
var replacedString = ""
arrs.forEach {
val pTag = "<p></p>"
replacedString = string.replace(pTag, it)
}
println(replacedString)
It should show <p data-id="1"></p>this<p data-id="2"></p>
, but it shows
<p data-id="2"></p>this<p data-id="2"></p>
insteadeinsjannis
02/24/2021, 9:16 AMghosalmartin
02/24/2021, 9:49 AMPattern
object to Regex
although given its a static I don’t think it’d make much of a differencexii
02/24/2021, 10:57 AMmending3
02/24/2021, 11:48 AMmending3
02/24/2021, 1:41 PMval string = "8% of 25 is the same as <img class=\"limit\">There are no heroes in a punk rock band <img class=\"limit\">"
val arraySearch = arrayOf("<img class=\"limit\">", "<img class=\"limit\">")
val arrayReplacement = arrayOf("<img class=\"another-limit\" data-id=\"1\">", "<img class=\"another-limit\" data-id=\"2\">")
arraySearch.forEachIndexed { index, it ->
string.replace(it, arrayReplacement[index])
}
println(string)
the string doesn't get replaced. still showing 8% of 25 is the same as <img class="limit">There are no heroes in a punk rock band <img class="limit">
where it should have been 8% of 25 is the same as <img class="another-limit" data-id="1">There are no heroes in a punk rock band <img class="another-limit" data-id="2">
123
02/24/2021, 2:24 PM`companion object {
val SORTING_COLUMNS = mapOf(X to "Y")
val DEFAULT_SORTING_COLUMN: String = SORTING_COLUMNS[X]!!
}`
Anything I could do here to avoid reductant !!
?frank
02/24/2021, 6:14 PM//Java
n >>= 1;
//Kotlin
n = n shr 1 // E.g. n shr= 1
Alfred Lopez
02/24/2021, 10:08 PMKevin
02/25/2021, 8:42 AMinclude
or recyclerView
?
this is probably will be long, but i'll try to keep it simple. so i have a layout, where in that layout there is 3 sub-menu clickable tab. the 3 sub-menu tab has about 90% same UI, where it's difference only on the icon image and title text. because i'm originally will use recyclerView
, i had create the sub-menu UI on different xml file.
i was thinking to use recyclerView
and set the itemCount
to 3, and then set each item UI differences(icon & text) programatically. but in that moment, i got a thought what if i create the whole 3 sub-menu tab on that separate xml file and then called the sub-menu tab file on main layout by using include
.
because of this, i'm quite confused, on which is recommended to use for a better approach on making layout UI? thank youGerard Bosch
02/25/2021, 6:21 PMdata class MyDataClass(private val myProperty: BigDecimal?) {
val myProperty: BigDecimal = myProperty
get() = field ?: BigDecimal.ZERO // <-- I want to just expose the non-nullable version of the property
}
👆 I've just tried the above as per what I could grasp from https://kotlinlang.org/docs/properties.html#getters-and-setters but can't make it work 😬
Is that possible to redefine a property accessor in such way? If it is, which syntax/construct should achieve it? Thx!nkiesel
02/25/2021, 8:05 PMEndre Deak
02/25/2021, 8:21 PMResult
class. I’m trying to go through a process which would look like:
• do step 1, if fail then return with an error
• do step 2 using step 1 result, if fail then return with an error
• …
so far what I coded is kind-of ugly:
runCatching { step1() }
.fold(
onFailure = { myCustomError("step1") },
onSuccess = { step1Result ->
runCatching { step2(step1Result) }
.fold(
onFailure = { myCustomError("step2") },
onSuccess = { ...
...
)
I’m pretty sure there’s a better way to do this - any suggestions?Endre Deak
02/25/2021, 8:21 PMResult
class. I’m trying to go through a process which would look like:
• do step 1, if fail then return with an error
• do step 2 using step 1 result, if fail then return with an error
• …
so far what I coded is kind-of ugly:
runCatching { step1() }
.fold(
onFailure = { myCustomError("step1") },
onSuccess = { step1Result ->
runCatching { step2(step1Result) }
.fold(
onFailure = { myCustomError("step2") },
onSuccess = { ...
...
)
I’m pretty sure there’s a better way to do this - any suggestions?nanodeath
02/25/2021, 8:25 PMval result1 = try { step1() } catch (e: Exception) { myCustomError("step1") }
val result2 = try { step2(result1) } catch (e: Exception) { myCustomError("step2") }
but with like a dozen more newlines in there. doesn't use Result but maybe you can say .get()
to force it to throw or something.Orhan Tozan
02/25/2021, 8:44 PMfun <T, R> Result<T>.letIfSuccess(block: (T) -> Result<R>): Result<R> =
when(this) {
is Result.Failure -> this
is Result.Success -> block(this.data)
}
Nir
02/25/2021, 8:51 PMrunCatching
Orhan Tozan
02/25/2021, 8:52 PMNir
02/25/2021, 8:54 PMval r = runCatching {
val x = someFunc().getOrThrow()
val y = someFunc2().getOrThrow()
}
etcOrhan Tozan
02/25/2021, 8:57 PMNir
02/25/2021, 9:09 PMfold
you don't really need runCatchingOrhan Tozan
02/25/2021, 9:16 PMNir
02/25/2021, 9:16 PMOrhan Tozan
02/25/2021, 9:16 PMNir
02/25/2021, 9:17 PMOrhan Tozan
02/25/2021, 9:18 PMNir
02/25/2021, 9:19 PMOrhan Tozan
02/25/2021, 9:20 PMNir
02/25/2021, 9:21 PMOrhan Tozan
02/25/2021, 9:22 PMNir
02/25/2021, 9:23 PMOrhan Tozan
02/25/2021, 9:24 PMNir
02/25/2021, 9:26 PMfold
, and then letIfSuccessOrhan Tozan
02/25/2021, 9:28 PMNir
02/25/2021, 9:28 PMOrhan Tozan
02/25/2021, 9:29 PMNir
02/25/2021, 9:29 PMval x = runCatching {
step2(step1())
}
val x = runCatching {
val first = runCatching { step1() }.getOrElse { // throw custom exception type }
val second = runCatching { step2(first) }.getOrElse { // throw custom exception type }
}
Orhan Tozan
02/25/2021, 9:31 PMAny
? Or is it a supertype of the custom error type and succeasfull path return typeNir
02/25/2021, 9:31 PMOrhan Tozan
02/25/2021, 9:33 PMNir
02/25/2021, 9:34 PMx
will be a Result, which is what OP wantsOrhan Tozan
02/25/2021, 9:34 PMNir
02/25/2021, 9:35 PMOrhan Tozan
02/25/2021, 9:36 PMNir
02/25/2021, 9:36 PMEndre Deak
02/25/2021, 10:03 PMmyCustomError
would return with a custom result object indicating the success of the entire process or the failure (at which step and what the issue was).
So if I’m flattening out, what I want to achieve nicely is something like:
data class MyCustomResult(
val code: ResultCode,
// .. details)
val firstRes = runCatching { first() }
if (firstRes.isFailure) {
return MyCustomResult(STEP_1, first.exceptionOrNull())
}
val secondRes = runCatching { second(firstRes.getOrNull()!!) }
if (secondRes.isFailure)
...
...
Nir
02/25/2021, 10:03 PMEndre Deak
02/25/2021, 10:04 PMNir
02/25/2021, 10:04 PMEndre Deak
02/25/2021, 10:06 PMNir
02/25/2021, 10:06 PMEndre Deak
02/25/2021, 10:08 PMgetOrElse()
returns with Any
runCatching { first() }
.getOrElse { firstError() }
.let { f -> second(f) }
.getOrElse { ... }
...
won’t work because f
is Any
instead of the result typeNir
02/25/2021, 10:26 PMResult
instead of MyCustomResultResult
-like machinery in MyCustomResult