https://kotlinlang.org logo
#announcements
Title
# announcements
n

Nir

07/09/2019, 8:42 PM
(I was also interested/surprised to see that variadic generics were not on the "top 20 wanted" list for Kotlin)
k

karelpeeters

07/09/2019, 8:57 PM
Could you share a usecase? I'm interested.
n

Nicholas Bilyk

07/09/2019, 9:08 PM
observer patterns would benefit from this. Currently you if you have system where you register callbacks, you need to create an interface to invoke the callback.
n

Nir

07/09/2019, 9:09 PM
@karelpeeters sure
k

karelpeeters

07/09/2019, 9:09 PM
You can also create a small class that encapsulates the event, then you only have a single generic parameter.
n

Nir

07/09/2019, 9:09 PM
So, here's an example
n

Nicholas Bilyk

07/09/2019, 9:09 PM
right, but in a case where there are thousands and thousands of callbacks (a typical UI framework), that's a lot of boilerplate
n

Nir

07/09/2019, 9:09 PM
Let's say you have multiple mutexes
n

Nicholas Bilyk

07/09/2019, 9:10 PM
when you often just want val changed: Signal<String, String> // old, new
n

Nir

07/09/2019, 9:10 PM
Instead of naively calling lock on each one, which can easily result in deadlocks between threads, a better approach is to write a function that takes all of the mutexes
and locks them in a consistent ordering
n

Nicholas Bilyk

07/09/2019, 9:10 PM
instead of val changed: Signal<ChangedEvent> and having to define that
n

Nir

07/09/2019, 9:10 PM
Now, this use case can be handled in Kotlin by taking an array of Lockable or something like that
but let's go a step further
it's very common for a mutex to be used to lock a specific piece of data
So imagine a generic class LockedData
Now, what we want to do, is have a variadic function, withLocks
It takes arbitrary number and types of LockedData's, and a lambda. It locks all of the mutexes in consistent order, and then calls the lambda with all the underlying objects
So for example, in C++, you could write code such that the user could do:
Copy code
LockedData<string> x;
LockedData<int> y;

withLocks(x, y, [&] (const string& p, int q) {
    // do stuff with p and q
}
k

karelpeeters

07/09/2019, 9:13 PM
Ah yeah, that's similar to the "?.let with multiple values at once" use case, I think there's an issue for that.
n

Nir

07/09/2019, 9:13 PM
Ah cool. if you have a link, let me know.
I've noticed in general that Kotlin looks very smooth when you're working with one of something. But when you have several of something, you notice that a) many of the "cool" Kotlin features really only work well with one thing at a time, e.g. extension functions, infix, and b) lack of variadics
A simple example is also using multiple resources at once
There's no canonical solution, and the best user-written solutions I've seen just don't look as nice as the single resource case