Chet
Chet

Reputation: 134

How to pass variable number of functions as parameters in Kotlin?

I'm new to Kotlin and i'm trying to refactor some code in Kotlin.

I have this piece of code that im calling in multiple places which i'd like to call a single function instead

Mockito.`when`(mockedSkillMaxCountRepository.getSkillMaxCount()).thenReturn(
     SkillMaxCount(count = 65),
     SkillMaxCount(count = 65)
)

And I want to do something like this where the number of parameters can be any number

mockSkillMaxCount(SkillMaxCount(count = 65), SkillMaxCount(count = 65),...)

private fun mockSkillMaxCount(SkillMaxCount(count = 65),SkillMaxCount(count = 65),...){       
     Mockito.`when`(mockedSkillMaxCountRepository.getSkillMaxCount()).thenReturn(params)
}

Upvotes: 1

Views: 178

Answers (1)

Andrei Tanana
Andrei Tanana

Reputation: 8422

You can use vararg modifier:

private fun mockSkillMaxCount(vararg skills: SkillMaxCount) {
    Mockito.`when`(mockedSkillMaxCountRepository.getSkillMaxCount()).thenReturn(*skills)
}

Upvotes: 1

Related Questions