How can I force calls to some constructors/functions to use named arguments?

In Kotlin 1.0 you can do this by using Nothing from the stdlib. In Kotlin 1.1+ you will get “Forbidden vararg parameter type: Nothing” but you can replicate this pattern by defining your own empty class with a private constructor (like Nothing), and using that as the first varargs parameter. /* requires passing all arguments … Read more

Collect to map skipping null values

Actually, a slight change to pwolaq’s answer guarantees that the second item is non-nullable: val map = listOf(Pair(“a”, 1), Pair(“b”, null), Pair(“c”, 3), Pair(“d”, null)) .mapNotNull { p -> p.second?.let { Pair(p.first, it) } } .toMap() println(map) This will give you a Map<String, Int>, since mapNotNull ignores anything that maps to null, and using let … Read more

break or return from when expressions

You can use run with a return at label: when(transaction.state) { Transaction.Type.EXPIRED, //about 10 more types Transaction.Type.BLOCKED -> run { if (transaction.type == Transaction.Type.BLOCKED && transaction.closeAnyway) { close(transaction) return@run //close if type is blocked and has ‘closeAnyway’ flag } //common logic } //other types }

Serializer for class ‘…’ is not found. Mark the class as @Serializable or provide the serializer explicitly

after reading some documentation I found that the error in my case was mainly due to two misconfigurations that I had: first: I needed to add the plugin in the gradle at the app and project level, I solved this as follows: adding in gradle.project the next line: id ‘org.jetbrains.kotlin.plugin.serialization’ version ‘1.6.21’ As is seen … Read more

Kotlin convert hex string to ByteArray

You can handle it like this: fun String.decodeHex(): ByteArray { check(length % 2 == 0) { “Must have an even length” } return chunked(2) .map { it.toInt(16).toByte() } .toByteArray() } Split the string into 2-character pairs, representing each byte. Parse each hex pair to their integer values. Convert the parsed Ints to Bytes.

Singleton with argument in Kotlin [duplicate]

Since objects do not have constructors what I have done the following to inject the values on an initial setup. You can call the function whatever you want and it can be called at any time to modify the value (or reconstruct the singleton based on your needs). object Singleton { private var myData: String … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)