24

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?

3
  • 6
    You'll want var pendingFunction: ((Double, Double) -> Double) to declare a variable of function type. Commented Apr 25, 2017 at 20:13
  • 1
    'var pendingFunction: ((Double, Double) -> Double) = { firstDouble, secondDouble in /* do you stuff here*/ }' then call it 'let resultDouble = pendingFunction(double1, double2)' Commented Apr 25, 2017 at 20:17
  • 1
    As for .self, that means you're assigning the type of the function, rather than an actual function. For example, var someInt: Int means you're going to assign a specific Int value to it, while var someInt = Int.self means you're using it to represent the Int type as a whole. Commented Apr 25, 2017 at 20:19

1 Answer 1

39

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"
Sign up to request clarification or add additional context in comments.

2 Comments

When I insert this line in the var class definition place I get a compiler error which appear on another innocent line. Xcode 11.3.
@DavidKramf It would help if you showed the error, the innocent line that's reporting it, and which line you inserted and where. You may need to post it as a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.