[swift-evolution] Replace the override keyword by 'extend' and 'replace' or add an annotation like @SuppressSuperCall
Haravikk
swift-evolution at haravikk.me
Wed Feb 17 08:55:11 CST 2016
> On 17 Feb 2016, at 01:22, Brent Royal-Gordon <brent at architechies.com> wrote:
>
> That's a serious problem for Swift resilience—it's not safe to change a public class from final to non-final because that would break devirtualized calls in other modules. Whatever the default is, it needs to not break things.
Swift hasn’t really shied away from breaking things thus far, but this is partly why I mentioned supporting a class level override. So for example, if @super(optional) is the current behaviour (can override, compiler doesn’t care what you do) then that could be added to all existing classes in a project, but without it a new class will default to its methods being final.
> - It conflates several different features—final, abstract, super call requirement, super call location—into a single set of annotations.
Not quite; I’d say that abstract is kind of its own thing, but would definitely build in the addition of extension requirements. Regarding the rest, is them being in a single set of annotations actually a bad thing? IMO that’s an advantage, as plus it’s not like it should be complicated. In the sample set of annotations I gave you might do:
@super(optional, before)
Which essentially reads as “extending classes don’t need to include super call, but must place it first if they do”. The most major change is really that the final keyword wouldn’t be necessary except for classes that set @super(optional) as their default (as everything would be overridable otherwise). That said it probably does make sense to just make final another type of requirement for consistency in that case.
> - It violates the usual grammar of Swift declarations by attaching particular modifiers directly to the keyword, disrupting the usual flow of modifiers-keyword-name-type.
private(set) already does this, so super(optional, before) or whatever wouldn’t be any different. We’re considering other options like @attributes as well, but they’re functionally the same so it really comes down to code style.
> - It not only supports, but actively encourages, placing extremely heavy constraints on subclass implementations. (Want to put a logging statement before a "before" method's super call or after an "after" method's? Too bad, the guy who wrote the superclass said "no”.)
I wouldn’t say it encourages heavy constraints, in fact I’d expect most developers to mostly use required or optional. The before and after conditions make more sense in the case of abstract classes that are partial implementations specifically intended to be built upon in a certain way.
It’s possible the restriction could be relaxed to allow non-mutating operations before/after the required statement, but I think in general developers shouldn’t be adding the before/after requirements unless they’re absolutely certain it has to be done that way. Technical you can still work around it like so:
class Foo {
super(before) func doSomething() { … }
}
class Bar : Foo {
super(required) override func doSomething() {
super.doSomething(); // Foo decreed that this shall be so
…
}
}
class AddLogging : Bar {
override func doSomething() {
/* Do some logging here and there’s nothing Foo can do about it */
super.doSomething()
}
}
Not pretty I know, but the point is that you aren’t actually completely prevented, but you should have a good reason to want to do this if it circumvents what a developer set as an explicit requirement. In the case of Foo being abstract however this actually makes a lot of sense, as Bar would be the first proper implementation, so has to follow all the rules, but relaxing them afterwards is fine.
> In general, this proposal seems like it's tremendously overengineered, insufficiently motivated, and fits poorly into Swift's syntax.
I don’t think the guts are actually that complex, the syntax just needs to be paired down to basics, and abstract classes possibly added later once the groundwork is done.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160217/43f3db1f/attachment.html>
More information about the swift-evolution
mailing list