[swift-evolution] [Proposal] Enums with stored properties

Charles Srstka cocoadev at charlessoft.com
Tue Oct 11 20:21:09 CDT 2016


> On Oct 11, 2016, at 4:42 PM, Braeden Profile via swift-evolution <swift-evolution at swift.org> wrote:
> 
> enum RectSize
> {
>    let height:Int
>    let width:Int
>    case small(width: 30, height: 30)
>    case medium(width: 60, height: 60)
>    case large(width: 120, height: 120)
> }

I like the concept, but this doesn’t seem as flexible as it could be, and could get ugly when mixing these defaults with enum associated values. How about dynamic properties instead? Something like:

enum RectSize {
	var height: Int { get }
	var width: Int { get }

	case small {
		height { return 30 }
		width { return 30 }
	}

	case medium {
		height { return 60 }
		width { return 60 }
	}

	case large {
		height { return 120 }
		width { return 120 }
	}

	case custom(width: Int, height: Int) {
		height { return height }
		width { return width }
	}
}

(syntax not exact; this is pseudocode, modify the syntax as appropriate)

This would keep the property implementations separate from the associated values, and would also allow for the computed properties to do more complex calculations if necessary.

Charles

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161011/c6160215/attachment.html>


More information about the swift-evolution mailing list