[swift-users] Compiler should issue a warning when a subclass implementation with default values matches a parent implementation without them

Wagner Truppel trupwl at gmail.com
Wed Jan 4 21:42:50 CST 2017


I’m afraid I wasn’t clear enough on my post. The default value I referred to is the “= 0”. Had it been absent, the call c.foo() would undeniably be fulfilled by the parent class. However, with the default argument value, it’s not obvious whether c.foo() should be the parent’s implementation or the child’s implementation (with the argument set to the default value). That’s why I’m suggesting a compiler warning because it’s very easy to make the mistake of calling c.foo() expecting the child’s implementation and it may be a hard bug to track when it happens.

Wagner


> On 5 Jan 2017, at 03:35, Saagar Jha <saagar at saagarjha.com> wrote:
> 
> I’m not quite sure what you mean by "restrictions of parent implementations”, however, the “default value” you’re mentioning is a fundamental part of OOP–when a subclass overrides a superclass, it gets the parent class’s methods for free. There’s no need to issue a warning for this, since it’s expected behavior from other Object-Oriented languages.
> 
> Saagar Jha
> 
> 
> 
>> On Jan 4, 2017, at 6:29 PM, Wagner Truppel via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> Hello,
>> 
>> I wasn’t sure whether to post this message here, at swift-dev, or at swift-evolution. so I’ll try here first. Hopefully it will get to the right group of people or, if not, someone will point me to the right mailing list.
>> 
>> I came across a situation that boils down to this example:
>> 
>> class Parent {
>>    func foo() {
>>        print("Parent foo() called")
>>    }
>> }
>> 
>> class Child: Parent {
>>    func foo(x: Int = 0) {
>>        print("Child foo() called")
>>    }
>> }
>> 
>> let c = Child()
>> c.foo()  // prints "Parent foo() called"
>> 
>> I understand why this behaves like so, namely, the subclass has a method foo(x:) but no direct implementation of foo() so the parent’s implementation is invoked rather than the child's. That’s all fine except that it is not very intuitive.
>> 
>> I would argue that the expectation is that the search for an implementation should start with the subclass (which is does) but should look at all possible restrictions of parent implementations, including the restriction due to default values.
>> 
>> At the very least, I think the compiler should emit a warning or possibly even an error.
>> 
>> Thanks for reading.
>> Cheers,
>> 
>> Wagner
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170105/6cbcdbd9/attachment.html>


More information about the swift-users mailing list