> Is the lack of comments due to general disinterest in such a thing or did my 
> mail go amiss somehow? ;)

I suspect it has something to do with the very similar thread I started about a 
week before you. :^) 
<https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160321/013153.html>
 Your use of `default` is new and very interesting, though.

I've actually been thinking about this a little more, and it occurred to me 
that we might be able to borrow Joe Groff's accessor idea from the property 
behaviors proposal. As you may recall, accessors are a way to specify a method 
(or, perhaps, a computed property) that can be implemented by the property to 
customize the behavior; accessors are implemented in a curly-bracket-enclosed 
block attached to the property. In property behaviors, they look something like 
this:

        var behavior getObserver<Value>: Value {
                accessor func willGet() {}
                accessor func didGet() {}
                
                var value: Value
                
                get {
                        willGet()
                        let v = value
                        didGet()
                        return v
                }
                set {
                        value = newValue
                }
                ...
        }
        
        @getObserver var x: Int {
                willGet { print("Getting x") }
        } 

Using the same mechanism in enums might remove the redundant boilerplate caused 
by redeclaring the same methods in every case, and also reduce the appearance 
that cases are types. Your example might look like:

        enum MyTree {
                mutating accessor func insert(value: T)
                
                case Leaf1(Float, Float) {
                        insert {
                                …
                        }
                }
                case Parent1(Int, Float) {
                        insert {
                                …
                        }
                }
                default {
                        insert {
                                …
                        }
                }
        }

Or, to use my example:

        enum Suit: Int {
                accessor var isRed: Bool { get }
                accessor var description: String { get }
                
                case hearts {
                        description { return "♥️" }
                        isRed { return true }
                }
                case spades {
                        description { return  "♠️" }
                }
                case diamonds {
                        description { return  "♦️" }
                        isRed { return true }
                }
                case clubs {
                        description { return  "♣️" }
                }
                default {
                        isRed { return false }
                }
        }

-- 
Brent Royal-Gordon
Architechies

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to