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

Arguments in JSClosure #155

Closed
mhavu opened this issue Jan 27, 2022 · 4 comments
Closed

Arguments in JSClosure #155

mhavu opened this issue Jan 27, 2022 · 4 comments

Comments

@mhavu
Copy link

mhavu commented Jan 27, 2022

How is one supposed to use arguments in JSClosure? Is it necessary to unpack them manually from the JSValue array? If I try to do that, I get an error message about ambiguous use of subscript(dynamicMember:):

JSClosure { (arguments: [JSValue]) in
    guard let item = arguments.first,  let deviceId = item.string else {
        // item in item.string causes error: ambiguous use of 'subscript(dynamicMember:)'
        return .undefined
    }
    // ...
}
@j-f1
Copy link
Member

j-f1 commented Jan 28, 2022

What Xcode/Swift version are you using? On Xcode 13.2.1 (swift 5.5), I’m seeing that code build without errors.

@mhavu
Copy link
Author

mhavu commented Jan 28, 2022

I have Xcode 13.2.1 (build version 13C100) and SwiftWasm 5.5.

@j-f1
Copy link
Member

j-f1 commented Jan 28, 2022

With this code, I get no errors (just a warning about variables being unused):

import JavaScriptKit

public struct test {
    public init() {
        let closure = JSClosure { (arguments: [JSValue]) in
            guard let item = arguments.first,  let deviceId = item.string else {
                // item in item.string causes error: ambiguous use of 'subscript(dynamicMember:)'
                return .undefined
            }
            return .number(42)
        }
    }
}

@mhavu
Copy link
Author

mhavu commented Jan 28, 2022

Thanks. Going through the cases one by one, I noticed that the problem was not with item but a typo in the member name after that (string in this case). By chance, I had picked the only one that actually worked in this case. 🤦‍♂️

@mhavu mhavu closed this as completed Jan 28, 2022
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