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

[SR-14834] Ideas for switch statements of struct/class with static member initializers #397

Open
swift-ci opened this issue Jun 27, 2021 · 0 comments

Comments

@swift-ci
Copy link
Contributor

@swift-ci swift-ci commented Jun 27, 2021

Previous ID SR-14834
Radar None
Original Reporter huntercmeyer (JIRA User)
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s SwiftSyntax
Labels New Feature
Assignee None
Priority Medium

md5: 976bf074a3f76098f22719cc2c027066

Issue Description:

This is my first post, so please let me know if I have any mistakes in the filing process 🙂.

I'd like to propose an idea for Swift. Essentially, my idea would pertain to the scenario when you have a struct/class when you have static members used for initialization. Imagine the following example:

struct Section {

    public private(set) base: Int

    static var main: Section {
        Section(base: 0)
    }

    static var secondary: Section {
        Section(base: 1)
    }
}

let section = Section.main

switch section {
    case .main:
        print("main")
    case .secondary:
        print("secondary")
    default:
        print("This should never happen")
}

When switching through an instance of Section, you must define a default case, even though the default case should never have to be called. One possibility would be the introduction of 'case' members that would replace the static member initializers. Imagine:

@exhaustible
struct Section {

    public private(set) let base: Int
    public private(set) let description: String? = nil

    case var main: Section {
        Section(base: 0)
    }

    case var secondary: Section {
        Section(base: 1)
    }

    case func tertiary(description: String) {
        Section(base: 2, description: description)
    }
}

let section = Section.main

switch section {
    case .main:
        print("main")
    case .secondary:
        print("secondary")
    case .tertiary(let description):
        print("tertiary: \(description)")
}

This implementation would (somehow) allow for the compiler to recognize that these methods should be used for switch statements and remove the need for default cases if the type uses a hypothetical `exhaustible` attribute.

An alternative idea would be to have an `Exhaustible` protocol:

protocol Exhaustible {
    static var cases: [Self]
}

struct Section: Exhaustible {
    public private(set) base: Int

    static var main: Section {
        Section(base: 0)
    }

    static var secondary: Section {
        Section(base: 1)
    }

    static var cases: [Section] {
        [.main, .secondary]
    }
}

let section = Section.main

switch section {
    case .main:
        print("main")
    case .secondary:
        print("secondary")
}

Then perhaps the compiler could (somehow) know that a default case is not needed. I do not know how this could look/work with static functions (such as the tertiary case).

Maybe this isn't a good idea or has been suggested before, but I couldn't find anything online. If anyone has feedback, I'd love to hear it! 🙂.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants