Skip to content

Functions as initializer arguments... #4

Description

@jeffc-dev

I have created a struct that takes a function as an initializer argument. This is then run periodically (using a timer) in order to repeatedly perform some logic on a periodic basis. The timer portion works fine (thanks for that!), but Skip seems to have trouble with the function argument.

A contrived and simplified example, omitting the timer aspect:

struct DoThing {
    var handler: (Int, Int) -> Int

    func run() {
        let result = self.handler(2, 5)

        print("result = \(result)")
    }
}

func addHandler(number1:Int, number2:Int) -> Int {
    return number1 + number2
}

func multiplyHandler(number1:Int, number2:Int) -> Int {
    return number1 * number2
}

let doThing1 = DoThing(handler: addHandler)
doThing1.run()

let doThing2 = DoThing(handler: multiplyHandler)
doThing2.run()

For this, Skip errors out with the following during compilation:

Function invocation 'addHandler(...)' expected
Function invocation 'addHandler(...)' expected
No value passed for parameter 'number1'
No value passed for parameter 'number1'
No value passed for parameter 'number2'
No value passed for parameter 'number2'
Type mismatch: inferred type is Int but (Int, Int) -> Int was expected
Type mismatch: inferred type is Int but (Int, Int) -> Int was expected
Function invocation 'multiplyHandler(...)' expected
Function invocation 'multiplyHandler(...)' expected
No value passed for parameter 'number1'
No value passed for parameter 'number1'
No value passed for parameter 'number2'
No value passed for parameter 'number2'

On iOS, this code results in the following (correct) output:

result = 7
result = 10

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions