Hi! I’ve got a weird case. Let’s take a sample fun...
# android
m
Hi! I’ve got a weird case. Let’s take a sample function:
Copy code
fun test(channel: List<String> = listOf(), channelGroups: List<String> = listOf())
Any idea how to set one parameter as “required”? I understand that the best way will be separate function to 2 different usecases, but in my scenario I need to accept: • not empty channel list • not empty channel groups • both not empty channel and channel groups
l
Just remove default value maybe
if you need input parameters to match some conditions, add appropriate checks
r
Remove default value, and it's become required.
z
Parameters can't be conditionally optional. You could use overloads as you suggested, but since the types of both params are the same you wouldn't be able to distinguish between the first two cases. If you don't mind the overhead, you could make a sealed class to represent all 3 cases. Or just do runtime validation instead and hope your consumers write adequate tests. Note that if you care about the lists being empty you'll have to write runtime checks anyway, there's no way to express that a list must not be empty in the type system.