[swift-users] Why can't Swift instance methods call class methods without qualification?

Rick Mann rmann at latencyzero.com
Fri Jul 1 16:32:26 CDT 2016

> On Jul 1, 2016, at 14:28 , Jens Alfke <jens at mooseyard.com> wrote:
> Sure you can. It’s easy to tell that it calls the instance method, because if it were calling the class method there would have to be a “Foo.” in front of it.

I'm saying you can't tell what was *intended*, at least, not without knowing about this particular idiosyncrasy of the language. But someone coming from C++ (and I imagine there are many), might not notice this without a compiler warning.

Now, if in this case, you had to be specific (either Foo. or self.), it might be okay. But I certainly would not make the assumption that the author didn't want to call the class method, and if I made the mistake in coding it, I would never know that's why, if it failed in some subtle way.

> You’re first arguing that class-method calls should have the same syntax as instance-method calls, and then complaining that having class and instance methods with the same name is ambiguous … but the reason for the ambiguity is because they’d be hard to tell apart using your proposed syntax. That’s not coherent.

I'm saying first and foremost that it should not be allowed to have a class and an instance method with the same name. That should just be a compile-time error.

Second, I'm saying that you should be able to call a class method on an instance.

Rick Mann
rmann at latencyzero.com

More information about the swift-users mailing list