[swift-evolution] [Pitch]: Default values for enum associated values

African Swift africanswift at gmail.com
Mon Jul 18 14:49:58 CDT 2016


Swift enum cases with associated values are quite similar to function
parameters, except that currently flexible specification with enum
associated values, is only possible by using Parameter Objects.

The proposed solution is to enhance Swift enum associated values to support
default values in a functionally equivalent way to default parameters on
functions.

<https://gist.github.com/AfricanSwift/bfeab7d930785ed5096939ce24bd6cbf#detailed-design>Detailed
design

For example, let's say we define a person case with associated values for
firstname and surname:

enum ... {
   case person(firstname: String, surname: String)
}

... then down the line let's say we want to extend it with a person's
age,  however
unlike functions, we're left only with imperfect options:

   - break the API
   - create a new case, e.g. person2(firstname: String, surname: String,
   age: Int)
   - re-implement case with a parameter object

Pitch

Should it be possible to assign defaults to *enum associated values*, with
a functional equivalence to function default parameter values (flexible
API), for example:

enum ... {
   case person(firstname: String, surname: String, age: Int? = nil)
}

Summary

Support defaults for enum associated values: to ensure that all the places
where the *associated value case* was used would continue to work when new
parameters (associated values) are added.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160718/ee162772/attachment.html>


More information about the swift-evolution mailing list