Uploaded image for project: 'Swift'
  1. Swift
  2. SR-14834

Ideas for switch statements of struct/class with static member initializers

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: SwiftSyntax
    • Labels:
      None

      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! .

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            huntercmeyer Hunter Meyer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: