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 PMkarelpeeters
09/16/2018, 12:35 PMthemishkun
09/16/2018, 12:47 PMComponent1<A>
, Component2<A, B> : Component1<A>
etc.karelpeeters
09/16/2018, 12:49 PMDico
09/16/2018, 12:58 PMinterface HasComponent1<out A> {
fun component1(): A
}
interface HasComponent2<out A, out B> : HasComponent1<A> {
fun component2(): B
}
... etc,
And these would be implemented by data classes implicitly?
I used the interface naming of HasComponent
as you can see, but it doesn't really matter either way.themishkun
09/16/2018, 1:04 PMDico
09/16/2018, 1:05 PMkarelpeeters
09/16/2018, 1:12 PMcomponentN
behaviour to be opt-in for data classes, like for data class Point(x: Int, y: Int)
it makes sense but less so for Person(adress: Adress, age: Int)
. But that's too late anyway.themishkun
09/16/2018, 1:16 PMDico
09/16/2018, 1:19 PMHasComponent2
for example to check if it's allowed with 2 variables, when it can also be a HasComponent3
or HasComponent4
. It would be weird. Anyway, it's not important. Just something small to consider for how the interfaces are implemented.themishkun
09/16/2018, 1:24 PMkarelpeeters
09/16/2018, 1:28 PMval (a, b) = list(1, 2, 3)
themishkun
09/16/2018, 1:29 PMDico
09/16/2018, 1:30 PMdata class Data(val a: Int, val b: Int, val c: Int)
fun test(data: Data) {
val (a, b) = data // no error
}
karelpeeters
09/16/2018, 1:31 PMList
and `data class`es have componentN
function, no difference as far as the compiler is concerned.Dico
09/16/2018, 1:33 PMList
, componentN
function is defined up to component5
. Maybe a little low for an upper limit for the rest of HasComponent
types.karelpeeters
09/16/2018, 1:34 PMcomponentN
functions.Dico
09/16/2018, 1:34 PMcomponentN
functions are operators. I didn't know that.karelpeeters
09/16/2018, 1:35 PMAndreas Sinz
09/16/2018, 1:48 PMDico
09/16/2018, 1:49 PMthemishkun
09/16/2018, 2:40 PMkarelpeeters
09/16/2018, 2:41 PMtodd.ginsberg
09/16/2018, 4:11 PMkarelpeeters
09/16/2018, 4:13 PMDico
09/16/2018, 4:20 PMthemishkun
09/16/2018, 4:25 PMComponentN
-s, I just referred to FunctionN current limit as some point to start evaluating. How did you choose this number when implementing FunctionN?todd.ginsberg
09/16/2018, 4:26 PMkarelpeeters
09/16/2018, 4:28 PMthemishkun
09/16/2018, 4:37 PMinline fun <A,B,C> Component2<A,B>.consumeBy(f: ((A,B) -> C)): C =
f(this.component1(), this.component2())
the usage is like with the spread operator for arrays: fun drawAt(x: Int, y: Int)
Point(1,2).consumeBy(::drawAt)
Dico
09/16/2018, 4:38 PMthemishkun
09/16/2018, 4:39 PMwhen (user) {
in User("johnDoe", any()) -> ...
in User(any(), 13) -> ...
else -> ....
}
You could think of that as Mockito matchers, if you never met pattern-matching beforekarelpeeters
09/16/2018, 4:41 PMin
to check each of the components, if contains
isn't defined? And what does any()
return here? Why are there User
objects here?themishkun
09/16/2018, 4:45 PMin (::User).pattern("johnDoe", any())
or any other syntactic trick that will make a Pattern
object and give it some matchers. I can create a gist for youkarelpeeters
09/16/2018, 5:20 PMany()
matches in a variable.Dico
09/16/2018, 6:33 PMequals
always returns true. However, that would violate equals contract, and it would have to be the LHS of the comparison. It should probably be a contextual keyword of some sort instead.
As for creating a Pattern
object, I guess that would be cached statically for when expressions? It might be better to just inline the comparisons.
Finally, what about:
in User(name = "mikhail")
This wouldn't be possible with the componentN
functions however, but it would make sense semantically.themishkun
09/16/2018, 8:34 PMvoddan
09/18/2018, 4:34 AM