[swift-evolution] [Proposal] Enums with stored properties
Kevin Nattinger
swift at nattinger.net
Wed Oct 12 15:48:19 CDT 2016
> On Oct 12, 2016, at 12:25 PM, Charles Srstka <cocoadev at charlessoft.com> wrote:
>
>> On Oct 12, 2016, at 12:32 PM, Kevin Nattinger via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> or more like a body (some different options for syntax, pick one obviously):
>> enum Size {
>> let height: Int
>> let width: Int
>> case small {
>> height = 30
>> width = 30
>> }
>> case medium: {
>> .height = 60
>> .width = 60
>> }
>> case large = {
>> let height = 120
>> let width = 120
>> }
>> }
>
> I sort of prefer to have a protocol-like syntax for declaring the properties, instead of let, because we’re doing something similar to what protocols do: we’re promising that each of these things will provide the property, although they might do it in different ways. This also avoids the connotation we have with “let” that the property will be a constant, when in actuality it may be dynamically computed and thus there is the possibility for it to be different each time.
My expectation is that the stored properties *are* constants, maybe even compile-time, though I’m open to runtime-initialized options. The whole point of the stored properties (as far as I’m concerned) is that they vary between cases but are always the same for a particular case, similar to int- or string-backed enums. If there’s a truly variable property, it should not be part of whatever associated storage backs the cases.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161012/0ed804b3/attachment.html>
More information about the swift-evolution
mailing list