karelpeeters
07/17/2018, 10:32 PMAllan Wang
07/18/2018, 4:22 AMfun Context.color(@ColorInt color: Int) = ContextCompat.getColor(this, color)
, came at a cost of a method count. I made a library similar to ktx back when I first started with Kotlin, and I had always thought that it would come at no cost to a developer because inlined functions were fully inlined. I guess if proguard strips away the unused methods that works too, but now I’m more conscientious about what I add since inline didn’t behave as I originally thought.Aregev2
07/18/2018, 8:19 PMfun Date.Companion.factoryExtension() {
}
diesieben07
07/18/2018, 8:19 PMthis
point to in such a method?karelpeeters
07/20/2018, 10:37 PMclass Matrix {
...
operator fun get(r: Int, c: Int) = ...
inner object Transpose {
operator fun get(r: Int, c: Int) = this@Matrix.get(c, r)
}
}
Allan Wang
07/20/2018, 10:44 PMclass Matrix {
val transpose by lazy { Transpose() }
...
inner class Transpose {
...
}
}
Transpose still doesn’t really behave like an object though, since it’s tied to another class.pp.amorim
07/21/2018, 4:35 PMpp.amorim
07/21/2018, 6:12 PMAregev2
07/21/2018, 8:12 PMgregd
07/24/2018, 1:15 PMlateinit
, but it only works with `var`s and non-primitive types).
What I mean is that ALL properties (val, var, objects, primitives) could be defined without the initial value. And in case of accessing non-initialized property we would get an error, e.g. “variable ‘x’ used before being initialized”arekolek
07/26/2018, 6:53 AMinterface Foo {
val bar: Int
fun getBaz(): Int
}
class Spam: Foo {
override fun getBar() = 0
override val baz get() = 0
}
I think what Andrey Breslav means is that to allow Kotlin properties override Java getters, this code would also have to be allowed to compile. I guess that if you look at Kotlin as a standalone language and not just "better Java" it makes sense that this code should not compile (but maybe there is a good reason for it to compile, other than Java interop, that I don't see?)arekolek
07/26/2018, 7:56 AMenum class Bit {
ZERO, ONE
}
fun test(bit: Bit): Int {
if (bit == Bit.ZERO) {
return 0
}
return when (bit) {
Bit.ONE -> 1
}
}
meaning exhaustive when
, where some branches have been exhausted before the when
? Would that be something useful?littlelightcz
07/28/2018, 10:18 AMrunCatching
feature and I am thinking about this use case: You want to process a list of files and after that you want to do a report about which file names succeeded and which didn't + what was the failure cause for each of them (or maybe possibly group failed file names by the same error etc.). Since now the Failure class contains only the exception, you lose the information for which original value it failed. Would it be possible to extend the Failure class to contain the failed value as well or is there a better way how to solve this use case?LeoColman
07/31/2018, 6:44 PMolek
08/06/2018, 7:45 AMnames.groupBy { it.length }
, one could use names.groupBy(String::length)
, but it's kinda cumbersome in many cases, especially when chained, and it's unclear what happens when the method has arity of 1. names.groupBy(::length)
would be shorter, but ::length
is evaluated in the current lexical scope. How about something like names.groupBy(.length)
? Scala has names.groupBy(_.length)
, Ruby has `Symbol#to_proc`: names.group_by(&:length)
. What do you guys think?Bernardo Ferrari
08/07/2018, 2:36 AMval str = "a😀cde"
val start = str.indexOf(str.first()) + 2
val end = str.indexOf(str.last()) - 2
val range = IntRange(start, end)
println(str.substring(range)) // "should output 😀c, but will output ?c"
Swift makes use of a Range<String.Index>, instead of Range<Int> (IntRange). The String Index is calculated based on a particular string so that it knows if there are any emoji or extended grapheme clusters. Don't you think would be nice to see a StringRange kind of thing on Kotlin?
Source: https://stackoverflow.com/a/35193481/4418073louiscad
08/07/2018, 9:51 AMasUnit()
method in the #stdlib so you can do things like override someMethodReturningJavaVoidOrUnit() = launch { ... }.asUnit()
?ske
08/09/2018, 12:18 AMalex.hart
08/15/2018, 6:48 PMclass Foo private constructor(private val barImpl: Bar): Bar by barImpl {
constructor() : this(BarImpl())
}
domfox
08/16/2018, 5:11 PMinline fun <T, K> Sequence<T>.groupByAndReduce(keySelector: (T) -> K, crossinline reducer: (T, T) -> T): Map<K, T> =
groupByAndReduceTo(LinkedHashMap(), keySelector, reducer)
inline fun <T, K, M : MutableMap<in K, T>> Sequence<T>.groupByAndReduceTo(destination: M, keySelector: (T) -> K, crossinline reducer: (T, T) -> T): M {
for (element in this) {
val k = keySelector(element)
destination.compute(k) { _, v -> if (v == null) element else reducer(v, element) }
}
return destination
}
inline fun <T, A, K> Sequence<T>.groupByAndFold(keySelector: (T) -> K, seedValue: A, crossinline folder: (A, T) -> A): Map<K, A> =
groupByAndFoldTo(LinkedHashMap(), keySelector, seedValue, folder)
inline fun <T, A, K, M : MutableMap<in K, A>> Sequence<T>.groupByAndFoldTo(destination: M, keySelector: (T) -> K, seedValue: A, crossinline folder: (A, T) -> A): M {
for (element in this) {
val k = keySelector(element)
destination.compute(k) { _, v -> folder(v ?: seedValue, element) }
}
return destination
}
Ruckus
08/22/2018, 4:40 PMFunctionX
.
I have a use case where I need a "recursive" function type, so I can't use a typealias:
typealias Interceptor<T, U> = (Store<T, U>, U, Interceptor<T, U>) -> Unit // Cannot be done
I can define a specific interface to replace that like so:
interface Interceptor<T, U> : (Store<T, U>, U, Interceptor<T, U>) -> Unit
but that interface cannot be used as a lambda:
fun <T, U> test(interceptor: Interceptor<T, U>) { ... }
// You have to do this
test(object: Interceptor<String, Int> {
override fun invoke(store: Store<String, Int>, value: Int, interceptor: Interceptor<String, Int> {
...
}
})
// I would like to do this
test<String, Int> { store, value, interceptor -> ... }
igor.wojda
08/22/2018, 10:33 PMAndroid Kotlin Guidelines
states that "When a function signature does not fit on a single line, break each parameter declaration onto its own line." From my observation there are certain cases where new line is also recommended - like (data) classes with multiple parameters in the constructor. Important advantage here is the fact that placing each parameter in new line makes code merge easier and often automatic. Because of that I wonder if someone before proposed to remove need of using ,
when method parameters are explicitly separated by new line?
data class Test (
val param1:String,
val param2:String,
val param3:String,
val param4:String
)
Proposal
data class Test (
val param1:String
val param2:String
val param3:String
val param4:String
)
chalup
08/31/2018, 3:05 PMInline classes are similar to type aliases, but they are not assignment-compatible with the underlying value type, so you cannot assign a String to a variable of type Name, and vice versa.I'm wondering about the
Name
to String
conversion part, i.e. let's say we have:
inline class Name(val value: String)
val name = Name("Jerzy")
// old API that still uses raw String
fun greet(val name: String)
// the way we'd call the method with Name
greet(name.value)
I'm curious what are the negative aspects of automatic unwrapping, i.e. allowing the greet(name)
call? I can't think of anything being terribly broken by this change, and it would allow gradual migration from bunch of String
params to wrapped classes.LeoColman
09/05/2018, 12:46 PMdiego-gomez-olvera
09/05/2018, 2:26 PMkrtko
09/06/2018, 8:16 PMweak var something: Something? = someRef //of type WeakReference<Something>
if (something == null) { /*True if something's reference is null*/ }
var inferredWeakSomething = something //of type WeakReference<Something>
strong var strongSomething = something //of type Something?
jkbbwr
09/07/2018, 3:38 PMjkbbwr
09/07/2018, 5:34 PM[1,2,3]
being both List<Int> and Set<Int> and Array<Int> depending on context.Dico
09/13/2018, 9:46 AMkarelpeeters
09/16/2018, 12:35 PM