bod
06/12/2021, 9:28 AMFuture<Void>
in a Kotlin library (so it's nice for Java users)? I'd like to avoid returning Future<Unit>
because this would expose a Kotlin specific type to my API.Xy Ren
06/12/2021, 10:21 AMwhen
. Is there any library dealing with this kind of problem or any idiomatic way? I tried to roll a dsl but it looked terrible:Tianyu Zhu
06/12/2021, 6:32 PMval myMap = mapOf("id" to 10, "hash" to 99999L)
I would expect the id
key to contain an Int
and the hash
key to contain a Long
, but that's not the case-- id
is now mapped to a long!
I can fix this by writing
val myMap = mapOf("id" to 10 as Int, "hash" to 9999L)
But then IntelliJ tells me that the cast is not needed.
Is there a better way?Toby
06/12/2021, 11:06 PME.Kisaragi
06/13/2021, 8:50 AMDaniel
06/13/2021, 7:43 PM10.seconds
to Duration.seconds(10)
I got an error that Duration.seconds
does not exist. Autocomplete shows the only members of Duration.Companion
are ZERO
and INFINITY
. However, jumping to the definition of Duration
shows fun seconds
. I don't understand what could cause thisTim McCormack
06/14/2021, 12:18 AMmvn test -Dtest="StateTest"
. I don't have the fastest computer, but I feel like this should take 30 seconds at most. Alternatively, is there some way I can do incremental compilation and/or testing? Anything to get the feedback loop down to seconds rather than minutes.Marcin Wisniowski
06/14/2021, 9:52 AMDavid Smith
06/14/2021, 11:00 AMsealed class Parent() {
data class Child(val value: String) : Parent()
}
data class Other(val value: (Parent) -> String)
val child = Child("something")
val f: (Child) -> String = { child -> child.value}
val x = Other(f as (Parent) -> String)
Slackbot
06/14/2021, 12:25 PMJérémy CROS
06/14/2021, 1:23 PMsealed class Sealed(open val x: Int) {
data class A(override val x: Int) : Sealed(x)
data class B(override val x: Int) : Sealed(x)
}
private fun copyTest(test: Sealed, y: Int): Sealed {
// "standard" way, it works
return when (test) {
is Sealed.A -> test.copy(x = y)
is Sealed.B -> test.copy(x = y)
}
// Sadly, this does not
return test.copy(x = y)
}
Is the when approach the only one? Or am I missing something?
Thanks!Arjan van Wieringen
06/14/2021, 2:17 PMdata class Node(val name: String, val onClick: () -> Unit)
fun main() {
val a = Node("Foo") { println("Foo") }
val b = Node("Foo") { println("Foo") }
println(a == b) // prints "false"
}
Is there a way to obtain something similar?Ruckus
06/14/2021, 4:59 PMinterface A {
val prop1: ...
val prop2: ...
...
companion object : A {
...
}
}
interface B : A {
val prop3: ...
...
companion object : B, A by A {
...
}
}
Is there a way to do this without the weird interface / companion construct?Patrick Ramsey
06/14/2021, 11:22 PMwhen (sendCommand(val result = command)) {
is Result.Success<CommandResponse> -> Result.Success(getDerivedValue(result))
is Result.SomeFailure<CommandResponse> -> Result.SomeFailure(result.message)
is Result.OtherFailure<CommandResponse> -> Result.OtherFailure(result.message)
}
I can’t think of a better way, but this feels incredibly boilerplatey — I’d love to be able to just return the underlying errors (since that’s what the caller will care about), but that doesn’t work since the generic type parameter is changing (even though the error subtypes don’t actually make use of the type parameter).
Alternatively, I could just hide the error, and present these fields as properties that are null if the lookup failed. But then I’ve robbed the caller of the ability to respond meaningfully to the failure.
TL;DR: is there any good pattern for forwarding a lower-level error up the call stack, where appropriate? Because I can certainly think of cases where that seems very appropriate.Pablo
06/15/2021, 10:42 AMfind
or firstOfNull
?
val foo = bar.firstOfNull {whatever}
if(foo==null) smthing()
else smthing2()
Kingsley Izundu
06/15/2021, 1:22 PMMrNiamh
06/15/2021, 1:38 PMprivate data class Quantity(val quantity: BigDecimal, val pendingQuantity: BigDecimal)
fun main(){
val quantities = listOf(Quantity(BigDecimal.ONE, BigDecimal.ZERO), Quantity(BigDecimal.ZERO, BigDecimal.TEN), Quantity(BigDecimal.ONE, BigDecimal.ONE))
println(quantities.sum()) //expecting Quantity(2, 11)
}
Obviously .sum()
doesn’t work, just an example of what i’d like it to look likeDanish Ansari
06/15/2021, 1:40 PMMap<String,String>
with case insensitive String
key?
More context and detailed scenario in the thread...kevindmoore
06/15/2021, 4:22 PMy9san9
06/15/2021, 8:25 PMobject Strings : LocalizedStrings {
val hello by string(default = "Hello") {
val ru by value("Привет")
val ua by value("Прівіт")
}
}
fun main() {
Strings.locale = "ru" // This should be also built in library with different realizations on each platform
println(Strings.hello)
}
Also would be nice to provide mutable state for compose, observable value for kvision, etc.Patrick Ramsey
06/16/2021, 1:25 AMsuspend inline
function (ie, inline fun <T> foo(() -> T) {}
works in a suspend context; inline fun foo(() -> Int) {}
does not). Am I correct about that? And if so, out of curiosity, what’s the implementation reason?althaf
06/16/2021, 3:19 AMfun foo() : Response {
workFlowLog?.let { Response.Sucess(BatchApprovalWorkFlowList(workFlowLog) ) } ?: run { Response.Error(Exception()) }
}
My intention is to return different response in case workflow is null. My team mates say this kinda of ugly. Is there a better kotlin idiom to craft this statement.
if( w == null) ... else ... , we are trying to avoid this.Mark
06/16/2021, 6:06 AM1. someString.replace(" ", "")
2. someString.filterNot { it == ' ' }
3. someString.filterNot(' '::equals)
missguru
06/16/2021, 3:50 PMDavid Smith
06/16/2021, 4:31 PMJsonNode
?E.Kisaragi
06/16/2021, 7:50 PMLastExceed
06/17/2021, 9:30 AMOrhan Tozan
06/17/2021, 10:11 AMDavid Smith
06/17/2021, 1:37 PMval NullableData? = firstNullable?.let { first ->
secondNullable?.let { second ->
NullableData(first, second)
}
}
I have something like that but with many more fields so it is massively nested. I suppose I could turn nulls into exceptions and surround the whole thing in a try catch? In Haskell/Purescript/Scala we would have do
notation for this and I think arrow had done something like this but I couldn’t find it?li'lfluf
06/17/2021, 10:44 PMpackage library.package
). is there a reason i shouldn't do this?li'lfluf
06/17/2021, 10:44 PMpackage library.package
). is there a reason i shouldn't do this?crummy
06/17/2021, 10:51 PMlouiscad
06/17/2021, 11:07 PMProxy
from Java reflection to handle the missing override and delegate for the other ones.li'lfluf
06/17/2021, 11:24 PMlouiscad
06/17/2021, 11:28 PMephemient
06/18/2021, 12:12 AM