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

Compile Error: using a function in @AppStorage() fails to compile if the enum type is inferred or if a parameter label is used #306

Open
lexum0 opened this issue Jan 4, 2025 · 0 comments

Comments

@lexum0
Copy link

lexum0 commented Jan 4, 2025

I found that when using a function as the @AppStorage key the function fails to infer the enum type causing an error (case 1, when typing 'Tab.tag(.welcome)').
By typing 'Tag.welcome' it fixes the error, although this is not needed outside the @AppStorage() wrapper.
But if you use a function parameter label it also fails to compile (case 3, when typing 'Tab.tag(key: Tab.welcome').

Here's a simplified test:

struct ContentView: View {
    ....

    // 1. Desired option. Doesn't work
    @AppStorage(Tab.tag(.welcome)) var selectedTab1: Int = 0
    
    // 2. Verbose option. It works
    @AppStorage(Tab.tag(Tab.welcome)) var selectedTab2: Int = 0
    
    // 3. Parameter labels don't work
    @AppStorage(Tab.tag(key: Tab.welcome)) var selectedTab3: Int = 0
}

enum Tab : String, Hashable {
    case welcome, home, settings
    
    static func tag(_ key: Tab) -> String { key.rawValue }
    static func tag(key: Tab) -> String { key.rawValue }
}
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

1 participant