[swift-evolution] Language syntax proposal - shorthand for super invocations
Radosław Pietruszewski
radexpl at gmail.com
Tue Feb 9 02:28:54 CST 2016
I want to say +1.
Ruby does this and it works really nicely. While not a big deal in simple cases, when a method has lots of arguments, it’s just really noisy.
There’s a problem though. If `super` means both “call the superclass’s implementation of this method”, and “the superclass”, there’s a potential for ambiguity.
Say:
> override var widgetCount: Int {
> return super.widgetCount + 5
> }
And:
extension Int {
var widgetCount: Int { return 10 }
}
Does super.widgetCount call the superclass’s `widgetCount`, or does it call the superclass’s implementation of the same getter, then call `widgetCount` on the returned Int?
— Radek
> On 09 Feb 2016, at 06:31, Petroules Jake via swift-evolution <swift-evolution at swift.org> wrote:
>
> Hello,
>
> I have a language syntax proposal. I've read the Swift evolution guidelines and hopefully this is the proper way to start. So, my proposal is:
>
> Permit a shorthand syntax for invoking the supertype implementation of a property or function. A single statement 'super' is equivalent to invoking the supertype implementation, forwarding the arguments unchanged, and returning the result. For example, the following samples:
>
> override func mouseEntered(theEvent: NSEvent) {
> super
> handleMouseEvent(theEvent)
> }
>
> override func specialValue(key: String) -> Int {
> return super + 1
> }
>
> override var widgetCount: Int {
> return super + 5
> }
>
> are exactly equivalent to the following samples:
>
> override func mouseEntered(theEvent: NSEvent) {
> super.mouseEntered(theEvent)
> handleMouseEvent(theEvent)
> }
>
> override func specialValue(key: String) -> Int {
> return super.specialValue(key) + 1
> }
>
> override var widgetCount: Int {
> return super.widgetCount + 5
> }
>
> Alternatives/complementary ideas:
>
> - Require `super()` or `super(...)` syntax for invocations which are function calls, for consistency with other function calls in the language and/or to indicate that arguments are forwarded
>
> Looking forward to your feedback.
>
> Thanks!
> --
> Jake Petroules - jake.petroules at theqtcompany.com <mailto:jake.petroules at theqtcompany.com>
> Consulting Services Engineer - The Qt Company
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160209/4a74b4cd/attachment.html>
More information about the swift-evolution
mailing list