mamaral / Neon

A powerful Swift programmatic UI layout framework.
MIT License
4.58k stars 389 forks source link

enums to OptionSets #11

Closed zdnk closed 8 years ago

zdnk commented 8 years ago

Right now:

avatarImageView.anchorInCorner(.BottomLeft, xPad: 15, yPad: 15, width: avatarSize, height: avatarSize)

could be:

avatarImageView.anchorInCorner([.Bottom, .Left], xPad: 15, yPad: 15, width: avatarSize, height: avatarSize)

Example of bitmask/option sets/flags in Swift 2.0:

struct MyOptions : OptionSetType {
    let rawValue: Int

    static let None         = MyOptions(rawValue: 0)
    static let FirstOption  = MyOptions(rawValue: 1 << 0)
    static let SecondOption = MyOptions(rawValue: 1 << 1)
    static let ThirdOption  = MyOptions(rawValue: 1 << 2)
}

let singleOption = MyOptions.FirstOption
let multipleOptions: MyOptions = [.FirstOption, .SecondOption]
if multipleOptions.contains(.SecondOption) {
    print("multipleOptions has SecondOption")
}
let allOptions = MyOptions(rawValue: 7)
if allOptions.contains(.ThirdOption) {
    print("allOptions has ThirdOption")
}
mamaral commented 8 years ago

The problem with that is, and the reason why I went with simple enums in most of these cases is, that corners and edges have specific configurations that make sense, and ones that don't make any sense. If you were to pass in [.Bottom] to anchorInCorner(), that wouldn't be enough information to do anything, and would require checking a number of invalid combinations and subsequent error handling and more complicated documentation. Passing in [.Bottom, .Left] to anchorToEdge() doesn't "make sense" either, so I still feel the current configuration is easier/clearer to implement and use.

zdnk commented 8 years ago

I see, you are probably right. It actually also provides build time check everything is all right.