[swift-evolution] Ad hoc enums / options

Hooman Mehr hooman at mac.com
Tue May 31 17:27:48 CDT 2016

If we go back to your original example:

func scaleAndCropImage(
    image: UIImage,
    toSize size: CGSize,
    fitImage: Bool = true
    ) -> UIImage {

There is a different type of sugar that I would like to have: Having label stand for `true` when we have a defaulted boolean flag, whose default value is false.

At call site we either say:

scaleAndCropImage(image: myImage, toSize: mySize)


scaleAndCropImage(image: myImage, toSize: mySize, fitImage)

We could still use:

scaleAndCropImage(image: myImage, toSize: mySize, fitImage: true)
scaleAndCropImage(image: myImage, toSize: mySize, fitImage: false)

Note that this is addressing a different situation: When we have multiple boolean flag parameters that can be combined like an OptionSet.

This is purely sugar: if the identifier matches the label of a defaulted to false boolean flag, it is interpreted as if the flag was passed as true. Other than that there is no effect.

I also like your idea as long as we put a limitation like what you proposed: Having it strictly be a UInt8 (or whatever) enum. Still the general resilience issues with enums remain and may get more complicated with this addition.

> On May 31, 2016, at 3:00 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>> On May 31, 2016, at 3:20 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
>>> func scaleAndCropImage(
>>>   image: UIImage,
>>>   toSize size: CGSize,
>>>   operation: (.Fit | .Fill) = .Fit
>>>   ) -> UIImage {
>> As I said the last time this proposal came up, I think this is great right up until the moment you need `operation` to be computed or kept in a variable. Then you need to start copying your anonymous enum type all over your source, and there's no central place to make changes. The same thing is true of tuples, but tuples are easy to structure and destructure from individual values on the spot; anonymous enums aren't really like that.
> And the obvious answer is you can have up to 255 of these babies for the anonymous enum type, and be able to pass numerical equivalents UInt8 with compile time substitution. That the ad-hoc enumeration is basically a syntactic shorthand for UInt8, with an enforced upper bound compile time check simplifies everything including switch statements.
> -- E
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

More information about the swift-evolution mailing list