[Proposal] Enums with stored properties


(J.E. Schotsman) #1

Wouldn’t it be more natural to allow structs as raw values?

For example

struct MyRect
  {
  var height:Int
  var width:Int
  var area:Int {return height:Int*width}
  }

enum RectSizes: MyRect
  {
  case Small(30,30)
  case Medium(60,60)
  case Large(120,120)
  }

let smallArea = RectSizes.Small.rawValue.area

Jan E.

···

On 10 Oct 2016, at 15:46,Mateusz Malczak wrote:

t a good illustration of what I would like to be able to define with
my proposed feature.
But instead to creating a class/struct to in switch case I would like
enumeration type to be able to carry that information within its cases


(Kevin Nattinger) #2

I would be amenable to that IF we can skip the `.rawValue`, which is, IMO, ugly and repetitive.

···

On Oct 10, 2016, at 11:54 AM, J.E. Schotsman via swift-evolution <swift-evolution@swift.org> wrote:

On 10 Oct 2016, at 15:46,Mateusz Malczak wrote:

t a good illustration of what I would like to be able to define with
my proposed feature.
But instead to creating a class/struct to in switch case I would like
enumeration type to be able to carry that information within its cases

Wouldn’t it be more natural to allow structs as raw values?

For example

struct MyRect
  {
  var height:Int
  var width:Int
  var area:Int {return height:Int*width}
  }

enum RectSizes: MyRect
  {
  case Small(30,30)
  case Medium(60,60)
  case Large(120,120)
  }

let smallArea = RectSizes.Small.rawValue.area

Jan E.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Mateusz Malczak) #3

For simple structs this is already possible (see this example:
https://swiftlang.ng.bluemix.net/#/repl/57fb8e3e4f9bcf25fdd415cd).
If we focus on more complex cases where properties of Any? type are
allowed I think this approach could do the trick...
but still I would like to be able to access properties without using rawValue

···

--

Mateusz Malczak

+-------------------------------

mateusz@malczak.info
http://malczak.info

2016-10-10 20:54 GMT+02:00 J.E. Schotsman via swift-evolution <swift-evolution@swift.org>:

On 10 Oct 2016, at 15:46,Mateusz Malczak wrote:

t a good illustration of what I would like to be able to define with
my proposed feature.
But instead to creating a class/struct to in switch case I would like
enumeration type to be able to carry that information within its cases

Wouldn’t it be more natural to allow structs as raw values?

For example

struct MyRect
{
var height:Int
var width:Int
var area:Int {return height:Int*width}
}

enum RectSizes: MyRect
{
case Small(30,30)
case Medium(60,60)
case Large(120,120)
}

let smallArea = RectSizes.Small.rawValue.area

Jan E.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution