(I was also interested/surprised to see that varia...
# announcements
n
(I was also interested/surprised to see that variadic generics were not on the "top 20 wanted" list for Kotlin)
k
Could you share a usecase? I'm interested.
n
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
@karelpeeters sure
k
You can also create a small class that encapsulates the event, then you only have a single generic parameter.
n
So, here's an example
n
right, but in a case where there are thousands and thousands of callbacks (a typical UI framework), that's a lot of boilerplate
n
Let's say you have multiple mutexes
n
when you often just want val changed: Signal<String, String> // old, new
n
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
instead of val changed: Signal<ChangedEvent> and having to define that
n
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
Ah yeah, that's similar to the "?.let with multiple values at once" use case, I think there's an issue for that.
n
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