Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three dots in Swift init method

I was looking at some Apple Combine source code and saw a Publisher name MergeMany with an init method defined like this:

public init(_ upstream: Upstream...)

What are the three dots ... after the Upstream? The value of Upstream is just another publisher as defined in the Struct definition.

public struct MergeMany<Upstream> : Publisher where Upstream : Publisher {
like image 560
Jon Vogel Avatar asked Apr 29 '26 14:04

Jon Vogel


1 Answers

The three dots mean that it is a variadic function and can take a list of Upstream. This is a function of the Swift Language and not the Combine Framework.

From Sergio Pietri's Blogpost:

Why should we use variadic functions? When you declare this type of function you are adding flexibility to the code. For example: the printf is probably the most notable function that uses this principle. The printf will take N amount of arguments and has many different parameters to format the output it will print.

And from the Swift documentation:

A variadic parameter accepts zero or more values of a specified type. You use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called. Write variadic parameters by inserting three period characters (...) after the parameter’s type name. The values passed to a variadic parameter are made available within the function’s body as an array of the appropriate type. For example, a variadic parameter with a name of numbers and a type of Double... is made available within the function’s body as a constant array called numbers of type [Double].

like image 64
WikipediaBrown Avatar answered May 01 '26 03:05

WikipediaBrown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!