Is there any talk about overloading enum cases with associated values?

I’m trying to find any talk on the evolution list about the overloading of enum cases with associated values.

Has this already been discussed?
Any technical reasons why we don’t have this already?
Anyone else would want this feature?
enum MyEnum {
     
    case a. // <—\
                // | should not be allowed!?
    case a(Int) // <—/
     
    // What I want is this:
    case javaScript(String)
    case javaScript(String, scope: Document)
    // instead of `case scopedJavascript(String, scope: Document)`
}
Best regards,

···

--
Adrian Zubarev
Sent with Airmail

I would love to have that! Unfortunately, I didn’t find the time yet to think it through and write a proposal. I often use enums for modeling server calls in small apps and prototypes, something along the lines of

public enum Client {

  private static let baseURLComponent = URL(string: "")!

  case foo(bar: String)
  case foob(bar: String, foobar: String)

  private func constructURL() -> URL {
    ...
  }

  public func fetch<T: JSONInitializable>(completion: (@escaping(T?, Error?) -> ())) {
    URLSession.shared.dataTask(with: constructURL()) { data, response, error in
      ...
      if let data = data {
        completion(T.init(json: JSON(data: data)), error)
      } else {
        completion(nil, error)
      }
      ...
    }.resume()
  }

}

- Dennis

···

On Nov 25, 2016, at 12:37 PM, Adrian Zubarev via swift-users <swift-users@swift.org> wrote:

I’m trying to find any talk on the evolution list about the overloading of enum cases with associated values.

Has this already been discussed?
Any technical reasons why we don’t have this already?
Anyone else would want this feature?
enum MyEnum {
     
    case a. // <—\
                // | should not be allowed!?
    case a(Int) // <—/
     
    // What I want is this:
    case javaScript(String)
    case javaScript(String, scope: Document)
    // instead of `case scopedJavascript(String, scope: Document)`
}
Best regards,

--
Adrian Zubarev
Sent with Airmail

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

1 Like