Better handling of enum cases with associated values


(Sash Zats) #1

Hi, I wanted to propose a better handling of enum cases with associated value
(somewhat) detailed proposal is here https://github.com/zats/swift-evolution/blob/master/proposals/0181-better-handling-of-enum-cases-with-associated-values.md
I'm not sure I can suggest good detailed implementation since I'm not too familiar with Swift internals
But i'm sure it's something I can research if proposal makes sense to community
Since I havne't communicated through this mailing list, I'm not sure what's the etiquette, let me know if something is missing from the proposal.


(Beta) #2

I like the spirit of this idea because it matches features found in other languages that have prominent pattern matching (ML, Haskell, etc.). But I’m concerned about the overloading of existing syntax for this case. Plus, if you find yourself destructuring large enum cases like this, have you considered making a struct containing those fields?

enum Value {
  struct Leaf {
    let data: Any?, moreData: Any?, evenMoreData: Any?
  }
  case container(leafs: [Value])
  case leaf(Leaf)
}

func foo(_ v : Value) {
  switch v {
  case let .leaf(x):
    print(x.data, x.moreData, x.evenMoreData)
  case .container:
    break
  }
}

~Robert Widmann

···

On Jul 10, 2017, at 8:35 AM, Sash Zats via swift-evolution <swift-evolution@swift.org> wrote:

Hi, I wanted to propose a better handling of enum cases with associated value
(somewhat) detailed proposal is here https://github.com/zats/swift-evolution/blob/master/proposals/0181-better-handling-of-enum-cases-with-associated-values.md
I'm not sure I can suggest good detailed implementation since I'm not too familiar with Swift internals
But i'm sure it's something I can research if proposal makes sense to community
Since I havne't communicated through this mailing list, I'm not sure what's the etiquette, let me know if something is missing from the proposal.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Sash Zats) #3

Oh I thought I replied to everyone :flushed:
Yeah I got the same comment on twitter - it seems like it's currently being handled through slightly different syntax, which works for me :+1:

···

On Jul 12, 2017, at 1:13 PM, Robert Widmann <rwidmann@apple.com> wrote:

I like the spirit of this idea because it matches features found in other languages that have prominent pattern matching (ML, Haskell, etc.). But I’m concerned about the overloading of existing syntax for this case. Plus, if you find yourself destructuring large enum cases like this, have you considered making a struct containing those fields?

enum Value {
  struct Leaf {
    let data: Any?, moreData: Any?, evenMoreData: Any?
  }
  case container(leafs: [Value])
  case leaf(Leaf)
}

func foo(_ v : Value) {
  switch v {
  case let .leaf(x):
    print(x.data, x.moreData, x.evenMoreData)
  case .container:
    break
  }
}

~Robert Widmann

On Jul 10, 2017, at 8:35 AM, Sash Zats via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Hi, I wanted to propose a better handling of enum cases with associated value
(somewhat) detailed proposal is here https://github.com/zats/swift-evolution/blob/master/proposals/0181-better-handling-of-enum-cases-with-associated-values.md
I'm not sure I can suggest good detailed implementation since I'm not too familiar with Swift internals
But i'm sure it's something I can research if proposal makes sense to community
Since I havne't communicated through this mailing list, I'm not sure what's the etiquette, let me know if something is missing from the proposal.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution