Edwar D Day
02/03/2022, 11:54 AMjlleitschuh
02/03/2022, 7:57 PMsmallufo
02/14/2022, 2:26 AMimport Three.Companion.toThree
@JvmInline
value class Three private constructor(val value: Int) {
init {
require(value >= 1)
require(value <= 3)
}
companion object {
private val array by lazy {
arrayOf(Three(1), Three(2), Three(3))
}
fun Int.toThree(): Three {
return if (this in 1..3) {
array[this - 1]
} else {
val index = (this % 3).let { r -> if (r == 0) 3 else r }.let { v -> if (v < 1) v + 3 else v }
array[index - 1]
}
}
}
}
fun main() {
println(1.toThree() == 4.toThree())
println(1.toThree() === 4.toThree()) // Identity equality for arguments of types Three and Three is forbidden
}
Online playground here :
https://pl.kotl.in/uBVYwgQgO?theme=darculaMichael Marshall
02/16/2022, 1:52 AMAny
, what is the default equals()
implementation, given I don’t override it myself?
The source code for Any
doesn’t show what the default is
public open operator fun equals(other: Any?): Boolean
Rob Elliot
02/22/2022, 8:46 AMkotlin.Array<E>
doesn't implement kotlin.collections.Iterable<E>
? They both define public operator fun iterator(): Iterator<E>
.
I know Java arrays don't, but does that limit Kotlin ones?
(I've often thought it would be convenient if Array<E> : FixedLengthMutableList<E>
& FixedLengthMutableList<E> : List<E>
, if that could all be done synthetically at compile time - is it reflection that prevents this? If so another reason to loathe reflection...)jw
02/24/2022, 3:43 AMRegex.matchAt
/ Regex.matchesAt
being stable? The issue which added the API (in 1.5.30?) was referenced on the issue for stabilizations in 1.6 but those APIs did not become stable in 1.6. Are things automatically bumped to be looked at in 1.7? Or is stabilizing in a 1.6.x an option? Does there need to be an issue tracking this?Javier
03/02/2022, 10:47 AMdropLastOrEmpty
(or dropLastOrNull
?Michael Marshall
03/03/2022, 6:55 AMfun interface MyInterface {
fun myMethod(): Int
}
which can be created with a lambda
val myInterface: MyInterface = { 4 }
but is there an equivalent for single abstract property interfaces? It could look like
fun interface MyInterface {
val myProperty(): Int
}
and be called like
val myInterface: MyInterface = 4
Javier
03/03/2022, 2:37 PMappendLines
in buildString
or the only way is something like list.forEach(::appendLine)
?Mustafa Ozhan
03/05/2022, 12:25 PMassertIs
method in whole instance checks in my tests, and I replaced many of
assertTrue { it is SomeClass }
with assertIs<SomeClass>(it)
Now, I am checking my coverage and it decreased, all the assertIs
methods seems partially covered
Is it expected? are they really partially covered ?Trevor Hackman
03/08/2022, 10:53 PMbinarySearchIndexed
With a signature of perhaps
fun <T : Comparable<T>> List<T>.binarySearchIndexed(
fromIndex: Int = 0,
toIndex: Int = size,
comparison: (T, Int) -> Int,
): Int
Thread in Slack Conversationrrva
03/14/2022, 12:36 PMval l = listOf(”P”, ”s”, ”P”, ”s”, ”s”, ”P”, ”P”, ”s”)
l.foo() -> [[”P”, ”s”], [”P”, ”s”, ”s”], [”P”], [”P”, ”s”]]
evkaky
03/17/2022, 7:49 AMnkiesel
03/23/2022, 7:52 PMhasContent
and noContent
which are overloaded for strings, collections, maps, stringbuilders, ... and treat null
as "no content". While switching to Kotlin, we try to use as much of Kotlin stdlib methods as possible. Our noContent
easily translates to isNullOrEmpty
, but hasContent
results in !isNullOrEmpty
which is not pretty, especially if that is at the end of a chain because it separates the !
from the method: !foo.bar.baz.isNullOrEmpty()
. Is there a place for isNotNullOrEmpty()
in stdlib? (And yes, I realize that once we finished our journey from Java to Kotlin, we can drastically reduce the usage of nullable types and thus often use isNotEmpty()
. But that is still a long and windy road).
Update: I should have searched for this in YouTrack before asking: was rejected in KT-33689 because it claimed we could "shortly" write `foo.bar.baz.!isNullOrEmpty()`as described in KT-5351 . I guess that time will only come though after K2 went GA.breandan
03/28/2022, 5:16 AMfilter
on Map<K, out V>
but not Set<T>
? Writing set.filter { ... }.toSet()
seems a little repetitive. https://kotlinlang.org/docs/collection-filtering.html#filter-by-predicatejaqxues
03/28/2022, 10:03 AMRob Elliot
03/30/2022, 10:09 AMfun <A, B, R> ((A, B ) -> R).curry(a: A): (B ) -> R = { b: B -> this(a, b ) }
fun <A, B, C, R> ((A, B, C) -> R).curry(a: A): (B, C) -> R = { b: B, c: C -> this(a, b, c) }
Do they already exist in the stdlib? Or are they a bit too Haskelly for Kotlin?Orhan Tozan
04/04/2022, 12:09 PMcontext(Foo)
fun bar() {
...
}
and
fun Foo.bar() {
...
}
?
If not, will the 2nd way of writing be eventually deprecated?nkiesel
04/04/2022, 5:50 PMnkiesel
04/04/2022, 10:07 PMfun String.startsWith(
prefix: Regex,
ignoreCase: Boolean = false
): Boolean
(and also endsWith
)? The ignoreCase
parameter would be debatable, but since the String
variant has it and it's easy to add to the implementation, I would vote to keep it.Sam Stone
04/07/2022, 10:01 PMKlitos Kyriacou
04/12/2022, 8:06 AMmapOf
unless it has to be a hashmap, in which case use hashMapOf
.
2. Use mapOf
to document to your readers that entry order is important, or hashMapOf
to document that order doesn't matter.Sam Stone
04/13/2022, 12:01 AMList.zip(List).toMap(mutableMapOf())
?Sam Stone
04/19/2022, 9:24 PMMutableCollection.addAll(Collection/Iterable/Sequence/Array
) faster if the input and/or output are certain types of collections?Sam Stone
04/20/2022, 12:52 AMghosalmartin
04/21/2022, 7:40 AMNon exhaustive 'when' statements on sealed class/interface will be prohibited in 1.7, add 'else' branch
Does that mean using a sealed class/interface will no longer be exhaustive in a when
. I thought that was a feature of ``sealed`?Adam Wilder
04/21/2022, 5:44 PM.mapNotNull
if possible? Or is it strictly a judgement call based on the size of the data expected since iterating over a list of size 2 twice is really not a big dealchristophsturm
04/22/2022, 8:25 AMChris Fillmore
04/25/2022, 5:44 PMkotlin.ranges.coerceAtLeast
and kotlin.ranges.coerceAtMost
, any reason these should be used in place of maxOf
/ minOf
? It surprises me that their implementation is not just Math.max
and Math.min
, but maybe I’m missing something!Junjie Wu
04/26/2022, 7:11 AMSequence<T>.drop(n: Int)
param type is Int
instead of Long
? I thought Kotlin’s sequence is equivalent to Java’s stream, and presuming the type should be the same as Stream<T> skip(long n);