Skip to content

createFunction not working with functions with 3 or 4 arguments #1010

Open
@alanhaw

Description

@alanhaw

sqlite.swift version: 0.11.6 - from ChangeLog.md, 0.12.2 from info.plist bundle version (downloaded Feb 20/2020)
XCode version: 11.3.1 (11C504)
macOS 10.14.6
sqlite.swift was integrated with the project manually

With the following code:

    func distance(num1: Int) -> Int {
        return 1
    }
    func distance(num1: Int, den1: Int) -> Int {
        return 2
    }
    func distance(num1: Int, den1: Int, num3: Int) -> Int {
        return 3
    }
            let test1: (Expression<Int>) -> Expression<Int> = (
                try db3!.createFunction("test1", deterministic: true) { anumer in
                    return self.distance(num1: anumer )
                }
            )
            let test2: (Expression<Int>, Expression<Int>) -> Expression<Int> = (
                try db3!.createFunction("test2", deterministic: true) { anumer, adenom in
                    return self.distance(num1: anumer, den1: adenom )
                }
            )
            let test3: (Expression<Int>, Expression<Int>, Expression<Int>) -> Expression<Int> = (
                try db3!.createFunction("test3", deterministic: true) { anumer, adenom, anum3 in
                    return self.distance(num1: anumer, den1: adenom, num3: anum3 )
                }
            )


The code compiles except that the definition of test3 reports an error at the beginning of "{ anumer".
It seems to be that createFunction works with 1 or 2 parameters, but not with 3 (nor 4, I've tried).
The error is: "Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored".

When I observe the expression lookup as I type in "db3.createFunction" it looks like there are an awful lot of typed definitions of createFunction. I am wondering if the multiple definitions are confusing the compiler, giving the error.
This may be specific to certain XCode versions [speculation].

This has been questioned on stackoverflow awhile back:
https://stackoverflow.com/questions/57383494/how-to-properly-define-closure-for-custom-function-in-sqlite

And to add an example with 4 parameters:

let dbdistance: (Expression<Int>, Expression<Int>, Expression<Int>, Expression<Int>) -> Expression<Int> = (
                try db3!.createFunction("dbdistance", argumentCount: UInt(4), deterministic: true) {num1, den1, num2, den2 in
                    return distance(num1: num1, den1: den1, num2: num2, den2: den2)
                }
            )

This returns a compile error of:
"Contextual closure type '([Binding?]) -> Binding?' expects 1 argument, but 4 were used in closure body"

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions