spand
02/12/2020, 8:59 AMXavier F. Gouchet
02/14/2020, 7:19 AMfun <K, V> MutableMap<K,V>.getOrCreate(key : K, create : ()->V?) : V? {
return if (containsKey(key) ) {
get(key)
} else {
put(key, create())
}
}
Fleshgrinder
02/14/2020, 2:35 PMwhen
expression to be exhaustive and it is exhaustive but the compiler cannot know, current example is java.nio.ByteOrder
which has exactly two instances but is not defined as an enum.Leon K
02/15/2020, 8:57 PMfun <T, K> List<T>.toMapBy(keySelector: (T) -> K): Map<K, T>
? i know there is .groupBy
but that returns a map of lists, while most of the time i'd want the equivalent of myList.map { it.myKey to it }.toMap()
Leon K
02/19/2020, 2:47 PM/** merge another map into this map,
* storing the result of applying the given remapping function to the two values if there is a key-conflict. */
fun <K, V> MutableMap<K, V>.putAllWith(other: Map<K, V>, remappingFn: (V, V) -> V) = other.forEach { (k, v) ->
merge(k, v, remappingFn)
}
any maybe a matching plus
-like operation (unionWith
?)elect
02/21/2020, 12:00 PMitems.list.filter { .. }.maxBy { it.depth }?.depth ?: -1
what about providing a maxOf((E)->T)
?Marc Knaup
02/26/2020, 5:03 AM@Suppress
? E.g. @Enforce
.
For example with non-exhaustive when
expressions:
‘when’ expression on sealed classes is recommended to be exhaustive, add ‘is …’ branch or ‘else’ branch insteadWith
@Suppress("NON_EXHAUSTIVE_WHEN_ON_SEALED_CLASS")
the warning is gone.
With @Enforce("NON_EXHAUSTIVE_WHEN_ON_SEALED_CLASS")
the warning will turn into an error.
That would allow catching missing branches earlier (e.g. when adding a new subclass to a sealed class).
It would even allow developers to turn any warning into an error for specific code, i.e. depending on the use case.
I guess that would be most useful for compiler warnings as the non-exhaustive when
warning is an inspection and wouldn’t cause the compilation process to actually fail 🤔Zach Klippenstein (he/him) [MOD]
02/26/2020, 11:58 AMClock
, ClockMark
, etc are being renamed to TimeSource
, TimeMark
, etc.
https://github.com/Kotlin/KEEP/issues/190#issuecomment-591081265Xavier F. Gouchet
03/03/2020, 11:00 AMsubstring
method :
fun String.substring(maxLength : Int): String {
return if (length > maxLength) substring(0, maxLength) else this
}
arekolek
03/04/2020, 11:21 AMhttps://blog.jetbrains.com/kotlin/2020/03/kotlin-1-3-70-released/
You know this convention in Kotlin: having a pair of functions, where the first one throws an exception if the operation isn’t possible, and the second one returnsIf this is a convention, then why there is no, likenull
andstring.toInt()
. Now we’ve added newstring.toIntOrNull()
andrandomOrNull()
counterpart functions, following the same conventionreduceOrNull()
max(): T
and maxOrNull(): T?
and instead we have a max(): T?
?
We had a discussion about it here https://kotlinlang.slack.com/archives/C1H43FDRB/p1551864308014200Slackbot
03/05/2020, 1:22 PMjw
03/13/2020, 3:10 PMResult
binary compatible with 1.3.70's? Or, put another way, how dangerous would it be to ship a library which uses Result
with 1.3.70 in anticipation of it being available for use in 1.4?LeoColman
04/03/2020, 3:06 PMlouiscad
04/16/2020, 2:31 PMfun CharSequence.occurencesOf
. It's shown as "Implemented" (with no version specified), but I'm not finding it in autocomplete in Kotlin 1.3.72. Is that a mistake or is there a way to access it?
https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/occurrences-of.mdmiha-x64
04/17/2020, 8:11 PMfun CharSequence.equalsIgnoreCase(that: CharSequence): Boolean {
val len = length
if (that.length != len) return false
return regionMatches(0, that, 0, len, ignoreCase = true)
}
Dealing with `CharSequence`s is pretty painful in Java, I'd like Kotlin to fix this…bod
04/23/2020, 6:02 PMindexOf
but with a default value (instead of -1). Is this something that exists? If not, I'm gonna make my own extension - what would you name it? 🙂natpryce
04/24/2020, 11:36 AMemptyList()
a generic function that returns a List<T>
, instead of a non-generic function that returns a List<Nothing>
? (Or even a constant or object?)miha-x64
04/24/2020, 4:30 PMobject EmptyList
? There are already Collections.emptyList()
and `List.of()`…miha-x64
04/25/2020, 4:24 PMLeon Linhart
05/02/2020, 10:48 PMinline fun <T> __apply(receiver: T, block: T.() -> Unit): T
(name tbd) which behaves excactly like the current`apply`.
Reasoning: When using Java libraries I tend to avoid extension functions with "simple" names (such as apply
, run
, let
, etc.) since it is always possible that a method with the same name is added to a class in a Java library. Due to Kotlin's resolution rules this could easily result in a source-incompatible change (depending on the exact method signature). However, with __apply(object)
this would not be a problem. Additionally, there is already a similar pattern in the stdlib: with
and run
. (See also https://kotlinlang.org/docs/reference/scope-functions.html#function-selection)
Bonus "real-world example": https://github.com/LWJGL/lwjgl3/issues/554Pedro González
05/03/2020, 3:12 AMgildor
05/08/2020, 3:51 AMmiha-x64
05/09/2020, 10:20 PMpublic infix fun IntProgression.step(step: Int): IntProgression {
checkStepIsPositive(step > 0, step)
return IntProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
}
else
branch is dead herebod
05/12/2020, 9:20 AMasad.awadia
05/15/2020, 3:46 PMmaxmello
05/21/2020, 2:03 PMmaxBy { it.someNullableValue ?: Long.MIN_VALUE }
.
This will return me the first value if the list is not empty, so I would have to append .takeIf{ it?.someNullableValue != null}
, which is fine for short cases like this, but when the value to compare is not top level but itself created by a more complex expression like it.someList.filter{…}.maxBy{…}?.someNullableValue
you need to write that same complex expression twice in the takeIf
part. So I wrote maxByOrNullIfNull
(kind of a bad name I know)
inline fun <T, R : Comparable<R>> Iterable<T>.maxByOrNullIfNull(selector: (T) -> R?): T? { // Allows selector to return R?
val iterator = iterator()
if (!iterator.hasNext()) return null
var maxElem: T? = iterator.next()
var maxValue: R? = selector(maxElem!!) // Here, we know maxElem is not null
if(maxValue == null) {
maxElem = null
}
if (!iterator.hasNext()) return maxElem
do {
val e: T = iterator.next()
val v: R? = selector(e)
if(v != null) {
if(maxValue == null) {
// If we don't have any non-null element yet, set the current element / value
maxElem = e
maxValue = v
} else if (maxValue < v) {
// Here, we know both values are not null, so we do the usual comparison
maxElem = e
maxValue = v
}
}
} while (iterator.hasNext())
return maxElem
}
I would argue that this would be a nicer default behavior for maxBy
, as it doesn’t really change the behavior for non-null cases, but makes clear that null values will always be considered “less” than any actual value in the comparison (no more need for ?: Long.MIN_VALUE
or something) and that we want an element with an actual value, not one that has no value for the comparison.Derek Peirce
05/22/2020, 7:15 PMIntRange.forEach
uses Iterable.forEach
, so (1..n).forEach { ... }
creates an Iterator
, while for (k in (1..n)) { ... }
is optimized to use a while-loop, not even creating the original IntRange
object. Could IntRange
receive its own forEach
extension method to preserve this optimization, and perhaps receive additional optimizations for common methods like map
?gsala
05/27/2020, 7:12 AMval impulseDownAllowed: Boolean = capabilitiesByte and 0x10.toByte() != 0.toByte()
val bleMovementAllowed: Boolean = capabilitiesByte and 0x20.toByte() != 0.toByte()
Bornyls Deen
05/27/2020, 8:16 PMfilterIsInstance
and first
but I imagine this is a little better performance wise as we are returning after the first match.
It's like first
is to filter
... `firstIsInstance`/`filterIsInstance` 😄
inline fun <reified T> Iterable<*>.firstIsInstance(): T? {
for (element in this) {
if (element is T) {
return element
}
}
return null
}
asad.awadia
06/03/2020, 8:44 PMasad.awadia
06/03/2020, 8:44 PMDominaezzz
06/03/2020, 8:53 PMmaxBy
and forEach
asad.awadia
06/04/2020, 12:17 AMDerek Peirce
06/06/2020, 8:16 PMmaxBy
does perform a check on each item, so you could perform the removal within that check.