Karlo Lozovina
02/25/2022, 7:33 PMnoneOf(T::class.java)
, noneOf(T::class)
work. If I pass around a value, then everything works fine (noneOf(value::class.java)
), but is there a way to keep everything on the type level?Advitiay Anand
02/25/2022, 10:58 PMYatin Shelke
02/27/2022, 12:10 AMSean Proctor
02/27/2022, 10:23 AMval result = foo.method1()
.let {
if (condition) {
it.method2()
} else {
it
}
}.method3()
Klitos Kyriacou
02/28/2022, 3:11 PMfun foo(args: IntArray) {
val a = intArrayOf(*args) // Creates a copy of args
//val b = intArrayOf(args) // Compile error
val c = intArrayOf(elements = args)
}
To pass an array to a varargs parameter, we are supposed to use the spread operator *
. But that creates a copy of the array, which you might not want to do if you are calling a pure function in a loop. You can't pass the array itself, but I learned that you can pass it if you use a named parameter! Where in the documentation does it say that?
[Update: looking at the generated bytecode, the call intArrayOf(elements = args)
also copies the array, and produces the same bytecode as using the spread operator.]Mark
03/01/2022, 2:30 AMval foos: List<Foo> = ...
foos.takeIf { it.isNotEmpty() }
to a function reference
1️⃣ foos.takeIf(List<Foo>::isNotEmpty)
2️⃣ foos.takeIf(List<*>::isNotEmpty)
3️⃣ foos.takeIf(Collection<Foo>::isNotEmpty)
4️⃣ foos.takeIf(Collection<*>::isNotEmpty)
5️⃣ leave it as a lambda毛小川
03/01/2022, 9:08 AM.forEach{it->}
guarantee the original sequence during execution?Anthony Flores
03/01/2022, 4:49 PMTimo Stark
03/01/2022, 7:25 PMgiven
stub working. Any Idea where to ask?Anthony Flores
03/01/2022, 7:57 PM(255, 255, 255, 255)toRGBA()
Turyin
03/02/2022, 11:27 AMPhani Mahesh
03/02/2022, 12:13 PMRob Elliot
03/02/2022, 3:32 PMResult
instances into a single Result? I can write it myself but a shame to repeat it.
I want roughly the following:
data class Foo(val a: String, val b: Int)
val resultA: Result<String> = TODO()
val resultB: Result<Int> = TODO()
val foo: Result<Foo> = reduce(resultA, resultB, ::Foo)
So the function would look something like:
fun <A, B, T> reduce(a: Result<A>, b: Result<B>, work: (A, B) -> T): Result<T>
But obviously it would need loads of overloads.Michael Langford
03/02/2022, 5:01 PMMason M
03/02/2022, 9:19 PMAyden
03/05/2022, 3:27 AMJDK 17
and Gradle 7.3.3/7.4
,
Unsupported class file major version 61
.
I think the solution to resolve this is to downgrade the JDK version to 11 after searching through the internet.
What should I do to resolve this issue if I insist to use JDK 17.
I am using WSL2, with the following setup for my console application.
JDK: 17
Build System: Gradle Groovy
Target JVM version: 16
Daeyoung Kim
03/05/2022, 7:50 AMChih Wei Lin
03/05/2022, 5:10 PMalthaf
03/08/2022, 12:08 PMwhen(args.approvalType) {
ApprovalType.MULTI_SELECT_BATCH_APPROVAL.ordinal,
ApprovalType.MULTI_SELECT_SINGLE_APPROVAL.ordinal -> {
context?.apply {
bgView.setBackgroundColor(getColor(R.color.sc_palette_green))
subTitleTextView.text = getString(R.string.approvalSuccessLabelText)
}
}
ApprovalType.MULTI_SELECT_SINGLE_APPROVAL.ordinal,
ApprovalType.MULTI_SELECT_SINGLE_REJECT.ordinal -> {
//TODO move to string resource
titleLineTitleTextView.text = "List of Transactions"
}
ApprovalType.MULTI_SELECT_BATCH_APPROVAL.ordinal,
ApprovalType.MULTI_SELECT_BATCH_REJECT.ordinal -> {
//TODO move to string resource
titleLineTitleTextView.text = "List of Batches"
}
ApprovalType.MULTI_SELECT_SINGLE_REJECT.ordinal,
ApprovalType.MULTI_SELECT_BATCH_REJECT.ordinal -> {
context?.apply {
subTitleTextView.text = getString(R.string.rejectSuccessLabelText)
bgView.setBackgroundColor(getColor(R.color.sc_palette_dark_blue))
}
}
}
Is this possible with when() to match two different blocks at the same time ?
Here i need to match multiple blocks are the same time, in different context, to show the text "List of Transactions" and "List of Batches"LastExceed
03/08/2022, 7:29 PMAnimal
and some inheriting classes Dog
Cat
etc, and i want each type of animal to have a lifeExpectancy
. now of course i could just create abstract val lifeExpectancy: Float
inside Animal
and have each child class override that, but that would result in every instance of Dog
holding a copy of the same number, which seems a bit redundant. (yes i know the overhead in this example is insignificant, but the problem is scalable). alternatively i could put the lifeExpectancy in the companion object, but then i can't generically look it up as the companion object can't be accessed generically (or at least i wouldn't know how to do that). i could store a reference to the companion object (or just the data i want to associate with the type in case it's not a primitive) in each instance to keep the overhead insignificantly low, but that would be incredibly ugly
is there any way to do this more elegantly?Trevor Hackman
03/08/2022, 10:06 PMbinarySearchIndexed
With a signature of perhaps
fun <T : Comparable<T>> List<T>.binarySearchIndexed(
fromIndex: Int = 0,
toIndex: Int = size,
comparison: (T, Int) -> Int,
): Int
allan.conda
03/09/2022, 1:29 AMMarko Novakovic
03/10/2022, 1:13 PMprivate fun UserContact?.isValid(): Boolean {
contract {
returns(true) implies (this@isValid != null)
}
return this != null &&
userId.isNotBlank() &&
!email.isNullOrBlank() &&
!phoneNumber.isNullOrBlank()
}
suspend operator fun invoke(): UserData {
val userData = getUserData()
val contact = getUserContact()
return when {
contact.isValid() -> userData.fillInWithUserContact(contact)
else -> userData
}
}
compiler is complaining contact
inside invoke
fillWithUserContact
is nullable. shouldn’t contract
ensure it’s not treated as nullable?Ayfri
03/11/2022, 4:34 AMround(number / length) * length
to round a number by a length ?No_One
03/11/2022, 10:40 AModay
03/11/2022, 2:00 PM"ticket-alert/remove/(?<id>.+)/(?<hash>.+)".toRegex()
Nat Strangerweather
03/11/2022, 6:27 PMfun savedData(): Array<Letter> {
val arr = mutableStateOf(boardLetters)
if (savedValues != null) {
for (item in savedLetters!!) {
arr.value = Array(28) {
Letter(item, 0)
}
}
}
return arr.value
}
For info: boardLetters is this: boardLetters: Array<Letter>
Thanks for any help.Andy Jiang
03/11/2022, 11:51 PMval a: Int = 100
val boxedA: Int? = a
val anotherBoxedA: Int? = a
val b: Int = 10000
val boxedB: Int? = b
val anotherBoxedB: Int? = b
println(boxedA === anotherBoxedA) // true
println(boxedB === anotherBoxedB) // false
Andy Jiang
03/11/2022, 11:53 PMval a: Int = 100
val boxedA: Int? = a
val anotherBoxedA: Int? = a
val b: Int = 10000
val boxedB: Int? = b
val anotherBoxedB: Int? = b
println(boxedA === anotherBoxedA) // true
println(boxedB === anotherBoxedB) // false
jcechace
03/12/2022, 6:24 PMjcechace
03/12/2022, 6:24 PMclazz.primaryConstructor!!.call(param)
When this line is executed from Kotlin code the primary consructor is returned. In Java I get a null value.wrongwrong
03/12/2022, 7:03 PMprimaryConstructor
to be null
even in a Kotlin
class.
import kotlin.reflect.full.primaryConstructor
class C {
val v: Int
constructor(v: Short) { this.v = v.toInt() }
}
fun main() {
println(C::class.primaryConstructor) // -> null
}
The primaryConstructor
will never be null
for a data class
.jcechace
03/13/2022, 3:48 PM