[swift-evolution] Ad hoc enums / options

Thorsten Seitz tseitz42 at icloud.com
Wed Jun 1 23:57:27 CDT 2016



> Am 31.05.2016 um 21:04 schrieb Erica Sadun via swift-evolution <swift-evolution at swift.org>:
> 
> 
>> On May 31, 2016, at 12:35 PM, Matthew Johnson <matthew at anandabits.com> wrote:
>> 
>> I think I'm -1 on this.  It makes things easier for the implementer of the function and harder for the caller.  
>> 
>> It's not clear whether the caller could store an argument to pass in a variable, but if they could they would need to list out all cases in the type of the variable (unless these anonymous enums have structural subtyping).  This is fragile.  Any time that list changes all variable declarations will have to be updated.
>> 
>> Functions are implemented once and usually called more many times.  It's better for callers if you just write it like this:
>> 
>> enum FitOrFill { case fit, fill }
>> func scaleAndCropImage(
>> image: UIImage,
>> toSize size: CGSize,
>> operation: FitOrFill = .fit
>> ) -> UIImage {
>> 
>> So unless these anonymous enums are structurally subtyped I think it's a bad idea.  And introducing structural subtyping here seems like a pretty large hammer for this use case.
> 
> 
> From the caller's point of view, the type must be inferable and exactly match a token listed in the declaration (which would also appear in Quick Help):
> 
> let _ = scaleAndCropImage(image: myImage, toSize: size, operation: .fill)
> 
> You would not be able to assign `.fill` to a variable and use that for the operation value.

That's a big negative point. This means that a small convenience for the implementor results in heavy restrictions for the caller who cannot structure his algorithm appropriately and will probably have to create (i.e. duplicate) the enum anyway.
Thinking of your example: where might the value for .fit or .fill come from? In most cases it won't be hardcoded in the call, but be either the result of a user setting or an algorithm. Both would need to be able to assign this value.

As a consequence this is just a feature for use in a Playground where it is very convenient, but that means it doesn't pull its own weight, because it degrades the design outside of Playgrounds.
So a -1 from me.

-Thorsten 

-Thorsten 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160602/0c75a89a/attachment.html>


More information about the swift-evolution mailing list