chrisdottel
chrisdottel

Reputation: 1153

Kotlin passing variable number of arguments to function

Google Maps has a function that allows you to pass any number of arguments to it. But I don't know how many arguments I want to pass in up front :(

Currently my code looks like this. It has arguments added in, but this is hardcoded. I want this to be variable. How can I do this in Kotlin?

polyLine= gMap.addPolyline(
            PolylineOptions()
                .add(_moments.get(0).position,
                    _moments.get(1).position,
                    _moments.get(2).position
                )
          )

Something like this?

polyLine= gMap.addPolyline(
            PolylineOptions()
                .add( for (i in 1..MAX_LIST_COUNT) {_moments.get(i).position} )
          )

Thanks for any help guys!

Upvotes: 1

Views: 255

Answers (1)

Roland
Roland

Reputation: 23352

You can use addAll instead:

PolylineOpions().addAll(_moments.map { it.position })

If you are just interested on how to pass something to a vararg method, then have a look at the spread operator for vararg functions. An example usage:

PolylineOptions().add(*_moments.map { it.position }.toTypedArray())

Upvotes: 3

Related Questions