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

Nicholas Outram nicholas.outram at icloud.com
Fri Jul 1 12:28:07 CDT 2016


I personally find the prefix to be consistent and safer.

class methods may mutate "mutable static variables” (singletons), which are dangerous in multi-threaded code. For me, having the class prefix is a reminder and therefore requires additional synchronisation if it is to be invoked safely from multiple threads.


Nick


> On 1 Jul 2016, at 01:59, Rick Mann via swift-users <swift-users at swift.org> wrote:
> 
> Why can my instance methods not call class methods without the class specifier?
> 
> class MyClass
> {
>   func
>   foo()
>   {
>       classMethod()
>   }
> 
>   class
>   func
>   classMethod()
>   {
>   }
> }
> 
> Why do I have to call MyClass.classMethod()? Just a choice made by the language designers to distinguish the call at the call site? I like C++'s way of treating all static methods as directly available to the instance.
> 
> -- 
> Rick Mann
> rmann at latencyzero.com
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list