Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions as initializer arguments... #4

Open
jeffc-dev opened this issue Jun 18, 2024 · 3 comments
Open

Functions as initializer arguments... #4

jeffc-dev opened this issue Jun 18, 2024 · 3 comments

Comments

@jeffc-dev
Copy link

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
@jeffc-dev
Copy link
Author

Adding this as a comment, as I'm not sure it's entirely relevant but that might provide some context.

What I'm working on here is a Swift package (Retriever) that allows you to provide an array of URLs to be periodically downloaded along with a set of options (e.g. frequency, order, error-checking, etc) that control how and when that download occurs.

This package can then be integrated into any app that needs to regularly poll for updates from a server.

I've written this kind of thing what seems like a million times over the years and figure that I should probably just make a generalized Swift package that I can reuse.

Basically, the developer using Retriever provides it with an array of Source structs, each of which can take - as an initializer argument - a function. This is run each time that Source's download completes. This allows the app to define its own logic to occur when a download completes (e.g. decode using JSONDecoder and assign to a variable somewhere), leaving the logic of controlling when/how those downloads happen to Retriever.

@aabewhite
Copy link
Contributor

Thanks for the report! Does it work if you initialize with a closure rather than a function reference?

@jeffc-dev
Copy link
Author

jeffc-dev commented Jun 18, 2024

Thanks for the report! Does it work if you initialize with a closure rather than a function reference?

I tested this and it seemed to work fine.

While I imagine you'll want to implement this at some point, it does take care of my near-term need to get this working.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants