[swift-evolution] [Idea] Extend "required" to methods other than init()

Howard Lovatt howard.lovatt at gmail.com
Mon Jan 18 13:34:54 CST 2016


>> 
>> Are there other languages worth investigating which have the requires super first/last semantics you note?
> 
> I don’t know of any languages that support this.  It is not the most common semantic, but when it occurs it seems important enough that it would be better if it could be specified in the language itself and enforced rather than left to documentation.

BETA has this ability, but it worked rather differently than modern OO languages (which work more like its predecessor Simula). In BETA you can only ever call the top most method, it can then optionally call the immediately overriding method. The syntax BETA uses is `inner(<args>)`. Probably easier with examples (in BETArised Swift):

    class Base {
        func finalM() { print("finalM") }
        func mustOverrideM() {
            print("beforeInner")
            inner()
            print("afterInner")
        }
    }
    
    class Derived: Base {
        func mustOverrideM() {
            print("inInner")
        }
    }

Methods `finalM` and `Derived.mustOverrideM` are automatically final because they do not call inner. 

You can't make an instance of `Base` because method `Base.mustOverrideM` needs to be overridden, i.e. `Base` is automatically abstract. 

If you:

    let d = Derived()
    d.mustBeOverriddenM()

Then it prints:

    beforeInner
    inInner
    afterInner



More information about the swift-evolution mailing list