Fast tuple-taking functions

Is it possible to pass a tuple to a function if their types match?

When I try, I get the missing argument in parameter error:

 var myTuple = ("Text",10,"More Text") func myFunction(a:String, b:Int, c:String) { // etc... } myFunction(myTuple) 
+7
source share
8 answers

This was possible, although Swift 2.2 was deprecated:

In Swift 2.1 and earlier, it was possible to use a carefully crafted tuple to populate function parameters. So, if you had a function that took two parameters, you could call it a two-element tuple if the tuple had the correct types and element names.

...

This syntax - affectionately called the "tuple syntax syntax" - is the antithesis to the self-documenting, readable style of the idiomatic Swifts, and is therefore deprecated in Swift 2.2.

https://swift.org/blog/swift-2-2-new-features/

+7
source

I came here wanting to learn how to pass a tuple as a parameter to a function. The answers here focus on another case. I do not quite understand what OP is.

In any case, here's how to pass a tuple as a parameter. And, for a good measure, how to do it in variations.

 func acceptTuple(tuple : (Int, String)) { print("The Int is: \(tuple.0)") print("The String is '\(tuple.1)'") } acceptTuple((45, "zebras")) // Outputs: // The Int is: 45 // The String is 'zebras' func acceptTuples(tuples : (Int, String) ...) { var index = 0 // note: you can't use the (index, tuple) pattern in the for loop, // the compiler thinks you're trying to unpack the tuple, hence /// use of a manual index for tuple in tuples { print("[\(index)] - Int is: \(tuple.0)") print("[\(index)] - String is '\(tuple.1)'") index++ } } acceptTuples((45, "zebras"), (17, "armadillos"), (12, "caterpillars")) //Outputs //[0] - Int is: 45 //[0] - String is 'zebras' //[1] - Int is: 17 //[1] - String is 'armadillos' //[2] - Int is: 12 //[2] - String is 'caterpillars' 

Passing tuples in can be a quick and convenient approach, which eliminates the need to create wrappers, etc. For example, I have a use case when I pass a set of tokens and parameters to create a game level. The tuple makes it nice and compact:

 // function signature class func makeLevel(target: String, tokens: (TokenType, String)...) -> GameLevel // The function is in the class Level. TokenType here is an Enum. // example use: let level = Level("Zoo Station", tokens: (.Label, "Zebra"), (.Bat, "LeftShape"), (.RayTube, "HighPowered"), (.Bat, "RightShape"), (.GravityWell, "4"), (.Accelerator, "Alpha")) 
+6
source

Yes, it is possible under these conditions:

  • the tuple must be unchanged
  • the number of values ​​in the tuple, their type and their order should correspond to the parameters expected by the function
  • Named parameters must match external names in the function signature
  • unnamed parameters must match parameters without an external name in the function signature

So, your code is fine, the only thing you need to do is turn the tuple into immutable (i.e. using let , not var ):

 let myTuple = ("Text", 10, "More Text") func myFunction(a:String, b:Int, c:String) { // etc... } myFunction(myTuple) 

Another example with external names:

 let myTuple = ("Text", paramB: 10, paramC: "More Text") func myFunction(a:String, paramB b:Int, paramC c:String) { // etc... } myFunction(myTuple) 
+5
source

In your tuple it seems that you should name them and then refer to them as such:

so your code should be

 var myTuple = (val1: "Text", val2: 10, val3: "More Text") func myFunction(a:String, b:Int, c:String) { // etc... } myFunction(myTuple.val1, myTuple.val2, myTuple.val3) 

The tuple has named values ​​(val1, val2, val3) that you set and then refer when passing myTuple to the myFunction () function, it looks like you just populate 1 of 3 available arguments - and with the wrong type to load! This is equivalent to storing types in a tuple, and then takes them out to call the function. However, if you want the function to actually take the tuple as a parameter, see below:

 var myTuple = (val1: "Text", val2: 10, val3: "More Text") func tupleFunc(a:(String, Int, String)) { } tupleFunc(myTuple) 
+1
source

Yes, but this is not a valid structure: you are passing three variables called a , b and c , not a tuple with these components.

You need parentheses around everything:

 var myTuple = ("Text", 10, "More Text") func myFunction(a:(x: String, y: Int, z: String)) { println(a) } myFunction(myTuple) 
0
source

In swift 3.0, we cannot pass a tuple directly to a function. If we did this, it displays an error message like "This type was removed in swift 3.0"

 func sum(x: Int, y: Int) -> Int return x+y } let params = (x: 1, y: 1) let x = params.0 let y = params.1 sum(x: x, y: y) 

Hope this helps you!

0
source

You can use the following function: Swift allows you to pass a function (f1) with any number of parameters (but without inout parameters) as a parameter of type (TIn) β†’ TOut for another function. In this case, TIn will be a set of parameters of the function f1:

 precedencegroup ApplyArgumentPrecedence { higherThan: BitwiseShiftPrecedence } infix operator <- :ApplyArgumentPrecedence func <-<TIn, TOut>(f: ((TIn) -> TOut), arg: TIn) -> TOut { return f(arg) } func sum(_ a: Int, _ b: Int) -> Int { return a + b } print(sum <- (40, 2)) 
0
source

This feature called implicit tuple splat been removed in Swift 3.

You can find a more detailed explanation of the removal proposal here.

Here are some tips to continue using the tuple as an argument:

 func f1(_ a : (Int, Int)) { ... } let x = (1, 2) f1(x) func f2<T>(_ a : T) -> T { ... } let x = (1, 2) f2(x) 
0
source

Source: https://habr.com/ru/post/976651/


All Articles