I have this function ``` fun findProfile(user: ...
# announcements
f
I have this function
Copy code
fun findProfile(user: UserDocument, vararg fields: String) { ... }
I'd like to make the vararg parameter required, because if I call that method like
userService.findProfile(user)
it doesn't complain. Any idea?
c
minimum number of arguments in a vararg is
0
, so no, you can't make it work like you want, except checking args length and throwing an
IllegalArgumentException
. What you can do (I know it's not very beautiful) is:
Copy code
fun findProfile(user: UserDocument, firstField: String, vararg additionalFields: String) { ... }
This will force your users to supply at least one field. And will look similar to what you want at the call site.
🎉 1
✔️ 1