Ayfri
09/08/2021, 9:39 PM.let
or similar functions but I can't come up with somethingdattq2303
09/09/2021, 11:10 AMabstract class AuthorizeSalesAdditionalSecurity {
@SerializedName("lang")
// Using like below is fine too
// abstract val lang: String
open val lang: String? = null
@SerializedName("POSTransactionID")
// abstract val POSTransactionID: String
open val POSTransactionID: String? = null
}
data class CreditAuthorizeSalesAdditionalSecurity(
@SerializedName("lang") override val lang: String,
@SerializedName("POSTransactionID") override val POSTransactionID: String
) : AuthorizeSalesAdditionalSecurity() {}
data class UnionPayAuthorizeSalesAdditionalSecurity(
@SerializedName("lang") override val lang: String? = null,
@SerializedName("POSTransactionID") override val POSTransactionID: String? = null
) : AuthorizeSalesAdditionalSecurity() {}
I realize that I can use keyword "open" or "abstract" in AuthorizeSalesAdditionalSecurity and Kotlin accept both. Can someone explain for me the difference between 2 use case?Justin Tullgren
09/09/2021, 4:34 PMIvan Brko
09/09/2021, 6:23 PMnkiesel
09/09/2021, 8:41 PMkoltin.time.Duration
property of a data class in Kotlin 1.5.30 using Jackson 2.12.5. One immediate problem is that - because Duration is a value class - Jackson sees just a long value and thus serializes a Duration.seconds(10)
as 20000000000
. This is obviously not what I want because it exposes the internal "use high bit to differentiate nano from milli". I tried to register a custom serializer which extends JsonSerializer<Duration>()
using @JsonSerialize
but that is ignored (presumably because Jackson just sees a long value). The only way I see right now is to have an explicit durationAsString
property and keep that in sync with the duration
property. Is there a better way? Note: I think I cannot use Kotlin serialization because this is part of a mixed Java/Kotlin project. But if I could: would that solve the issue?Jan
09/10/2021, 7:59 PMval buckets = mutableMapOf<String, RestClient.Bucket>()
suspend fun queue(endpoint: String, task: suspend () -> HttpResponse): HttpResponse {
println(buckets.size) //always 0
//....
send(endpoint, task)
}
private suspend fun send(endpoint: String, task: suspend () -> HttpResponse): HttpResponse {
buckets[endpoint] = bucket
println(buckets.size) //always one
}
Ayden
09/11/2021, 1:42 AMval
?suhas
09/11/2021, 3:14 AMids.forEach { id ->
val person = persons.find { it.id == id}
filteredList.add(person)
}
evkaky
09/11/2021, 6:49 AMrepeat(3) {
if (it == 0) return@repeat
println("inside repeat")
}
it prints “inside repeat” twice instead of 0 times
Why is that?Daniel Branco
09/11/2021, 10:01 AMdata class Foo(
var id: Int?,
var name: String = "bar",
var anotherVar
)
I thought I could have calls like Foo()
or Foo(id=1)
without needing to pass all the parameters to the constructor, am I wrong? Where can I read more about constructors in Kotlin?CLOVIS
09/11/2021, 10:07 AMIvan Brko
09/11/2021, 10:40 AMwhen
, so that i could write something like this:
when (expression generating nullable){
null -> handle null
x -> handle x for which compiler knows its not null anymore
}
suhas
09/12/2021, 2:17 AMif (mNotificationManager == null) {
mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
Daniel Branco
09/12/2021, 5:40 PM@Nested
, inner
classes, ParametrizedTests + MethodSource
I have the following
class MyServiceTest {
data class TestData(...)
@Nested
inner class MyServiceSpecific{
private fun prepareTestData() = Stream.of(...)
@ParameterizedTest
@MethodSource("prepareTestData")
fun `GIVEN this example WHEN executing test THEN should run`(data: Testdata) {
// EVERYTHING I WANT TO TEST
}
}
}
With this structure I get the following error
Cannot invoke non-static method [private final java.util.stream.Stream... on a null target.
How can I gave all this working, I am missing a glue probablylesincs
09/13/2021, 12:27 PMPriNova
09/13/2021, 12:27 PMtypealias func = ()-> Unit
@JvmField val command = mutableListOf<func>()
fun main() {
add {
val i =4
println("${i}")
}
eval(command[0])
}
fun add(action: () -> Unit): Unit {
command.add(action)
}
fun eval(action: () -> Unit) {
action()
}
Thank youManuel Dossinger
09/13/2021, 1:11 PMdata class MutableClass(var my1, var my2, var my3...)
and a fitting data class ImmutableClass(val my1, val my2, val my3...)
. So the only difference is, that all members of one class are var
and the members of the other one are val
and of course I don't want to write the list of members twice 🙂Karlo Lozovina
09/13/2021, 6:36 PMBase
, Is there a way to run some code before and after every method call on Base's subclasses?Slackbot
09/14/2021, 9:03 AMAnil Kumar
09/14/2021, 4:30 PMBrian Donovan
09/15/2021, 6:43 PMAsq
09/15/2021, 7:06 PMinline fun <reified R, reified S> reifiedGenericIsSame(r: R, s: S): Boolean = (r is S) && (s is R)
interface FooBar<out A: Any, out B: Any>
typealias FBOI<A> = FooBar<Int,A>
typealias FBOS<A> = FooBar<String,A>
and the following:
open class Base<out A: Any, out B: Any>(val a: A, val b: B): FooBar<A, B>
class SpecialOfInt<out B: Any>(b: B) : Base<Int, B>(1, b)
class SpecialOfStr<out B: Any>(b: B) : Base<String, B>("1", b)
// val soitd: FBOI<Int> = SpecialOfInt(2)
// val sostd: FBOS<Int> = SpecialOfStr(2)
val soi = SpecialOfInt(2)
val sos = SpecialOfStr(2)
//
// EDIT
//
val soitd: FBOI<Int> = soi
val sostd: FBOS<Int> = sos
then this holds:
reifiedGenericIsSame(soi, sos) shouldBe false // passes
reifiedGenericIsSame(soi, sos) shouldBe true // FAILS
reifiedGenericIsSame(soitd, sostd) shouldBe false // FAILS
reifiedGenericIsSame(soitd, sostd) shouldBe true // passes
which 🤞is VERY counterintuitive, as it appears a typealias (allegedly syntax sugar) is changing a result semantically. I suppose type erasure side effects may be invoked as the mechanics, but this question is about the intrinsic behavior. Could please anyone provide me with great kotlin wisdom?Travis Griggs
09/16/2021, 12:30 AMlistOf<DragGesture>(DragCyclePositionGesture.class, DragMoveGesture.class, DragBeginGesture.class, DragEndGesture.class)
is not legal. Number one, there needs to be way a way to make the template not be instances of DragGesture, but rather subtypes of DragGesture? Is that even specifiable? And the compiler isn't happy after the first .class either, i start getting all kinds of red after that. Am I supposed to use companions in this case? I'm not even sure how companions fit in the scheme of inheritance. 😕Gopal S Akshintala
09/16/2021, 12:51 PMrm -rf ~/.gradle/caches
and Invalidate Caches and restart on IntellijKarlo Lozovina
09/16/2021, 3:10 PMArray<IntArray>
? I'm trying to store a 2D matrix of ints/floats in a compact way, would this Array of IntArrays be the way to go? Would that be equivalent to int[][]
?Brian Donovan
09/16/2021, 3:56 PMNorbi
09/16/2021, 5:17 PMfun <T : Any> isNotNull(value: T?): Boolean {
contract {
returns(true) implies (value != null)
returns(false) implies (value == null)
}
return value != null
}
class ContractTest {
@Test
fun test1() {
val a: String? = null
if (isNotNull(a)) {
println(a.uppercase()) // Works as expected: "Smart cast to kotlin.String"
}
val notNull = isNotNull(a)
if (notNull) {
println(a.uppercase()) // Compilation error: "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?"
}
}
}
elect
09/17/2021, 8:45 AMkotlin.time.Duration
from something like d-hh:mm:ss
?
Combining d.toDuration(DAYS) + hh.toDuration(HOURS)
and so on?
If yes, I find java Duration
better in this regards, because you can parse it from a `String`: Duration.parse("..")
Danish Ansari
09/17/2021, 2:07 PMDouble
up to certain decimal points, there is round()
function in Kotlin but it always completely rounds up to 0 decimals? I want something like round(1.4999,decimalPrecision = 2) // 1.50
Viktor VAD
09/18/2021, 11:17 AMViktor VAD
09/18/2021, 11:17 AMandylamax
09/18/2021, 11:18 AMViktor VAD
09/18/2021, 11:21 AMedrd
09/19/2021, 1:25 PM