Swift: Private enum cases

Originator:rix.rob
Number:rdar://22891351 Date Originated:28-Sep-2015 11:06 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-beta (7B75)
Classification:Enhancement Reproducible:Always
 
Summary:
I have an enum which has several cases. Most of these are straightforward and obvious in use (correct-by-construction), but one or two have some invariants which can’t be expressed via the types. I would like to be able to make these latter cases private, restricting construction of such instances to wrapper constructors (functions or `init`s) which maintain the invariants correctly.

At the same time, these cases would need to remain publicly available for pattern matching to be total (exhaustive). So the access control keyword would have to apply to its use as a constructor alone, and not to pattern matching.

For the curious, the type in question is the DOC type described in Wadler’s A Prettier Printer: http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf — the union of two documents is defined as:

indirect case Union(Doc, Doc)

However, two invariants need to be maintained:

1. Union(x, y) requires that x and y must flatten to the same layout (i.e. flattening x and y to a single line of text results in an equal line of text).
2. The first line of every document in x must be at least as long as the first line of every document in y.

This cannot be guaranteed by arbitrary callers, but it can be guaranteed by functions such as the `group` function defined in the paper.


Steps to Reproduce:
N/A

Expected Results:
N/A

Actual Results:
N/A

Regression:
N/A

Notes:
N/A

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!