Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: assigning function to variable

Tags:

I have a class in swift with the following variable:

var pendingFunction = ((Double, Double) -> Double) 

Swift then tells me:

Expected member name or constructor call after type name

It insists that I change my code to:

var pendingFunction = ((Double, Double) -> Double).self 

I have no clue what this .self thing is doing (sorry I'm new to Swift)

I then try to assign pendingFunction to a new function:

pendingFunction = function 

where function takes two Doubles and returns a Double.

However, I'm presented with the following error:

Cannot assign value of type '(Double,Double) -> Double' to type '((Double,Double)->Double).Type'

So my question is: what is the .self thing doing and how can I properly assign a function to the variable?

like image 721
James Dorfman Avatar asked Apr 25 '17 20:04

James Dorfman


People also ask

How do you assign a variable to a function in Swift?

To assign function to a variable in Swift, declare a variable that takes specific set/type of parameters and return a specific type of value. Then we can assign a function that has same type of parameters and return value to this variable.

Can you pass a function as a parameter in Swift?

Every function in Swift has a type, consisting of the function's parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions.

How does a function return a value in Swift?

If you want to return your own value from a function, you need to do two things: Write an arrow then a data type before your function's opening brace, which tells Swift what kind of data will get sent back. Use the return keyword to send back your data.

What is Inout in Swift?

Swift inout parameter is a parameter that can be changed inside the function where it's passed into. To accept inout parameters, use the inout keyword in front of an argument. To pass a variable as an inout parameter, use the & operator in front of the parameter.


1 Answers

The postfix .self expression just refers to the object it's an instance of. Kind of like .this in other languages. For types in particular, it tells the compiler that you're referring to the type itself, rather than instructing it to create a new instance of that type. If you'd like to know more, you can read all about it in the docs here. While useful in a lot of cases, it's not really needed here.

As for your problem, when you assign:

var pendingFunction = ((Double, Double) -> Double).self 

You're assigning the type of a particular sort of function as the value of the variable. From that, Swift infers that the type of the variable should be Type. Then later when you try to assign an actual function fitting that type as the value, it throws an error because it's expecting a type, not a function.

Instead of assigning a type as value, you want to declare the variable with that type as its type:

var pendingFunction: ((Double, Double) -> Double) 

Here's an example of the whole thing:

var pendingFunction: ((Double, Double) -> Double)  func myAdditionFunction (first: Double, second: Double) -> Double {     return first + second }  pendingFunction = myAdditionFunction  print(pendingFunction(1,2)) // prints "3.0" 
like image 172
Robert Avatar answered Oct 08 '22 01:10

Robert