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

Kerry Hazelgren hazelgren at me.com
Fri Jul 1 13:12:42 CDT 2016


> On Jul 1, 2016, at 11:06 AM, Kate Stone via swift-users <swift-users at swift.org> wrote:
> 
>> On Jul 1, 2016, at 11:01 AM, Jens Alfke via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> 
>>> On Jul 1, 2016, at 10:28 AM, Nicholas Outram via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>> 
>>> class methods may mutate "mutable static variables” (singletons), which are dangerous in multi-threaded code.
>> 
>> This is the same argument zh ao made. But instance methods may also mutate static variables, so the fact that you’re calling a class method doesn’t make the call any more dangerous.
>> 
>> Moreover, mutating instance variables can be just as dangerous for multithreaded code (and is a more frequent source of bugs IMHO).
>> 
>> Really, the only significant difference is that a class method can’t modify instance variables, so from that perspective it’s actually a bit safer than an instance method call!
> 
> Though that isn’t strictly true for singleton patterns and other reasons why class variables might include references to instances.
> 
> I believe there’s real value in being explicit about referencing class members.  It helps both the reader of the code and it makes writing code with typical IDE conveniences like code completion less cluttered and more informative. Unfamiliar class methods won’t be included in lists of suggestions where they might look like they operate on the current instance.

I agree. I think that removing the class specifier provides a bit of simplicity but at the cost of ambiguity, which is decidedly not swift-y.

Kerry

> 
> Kate Stone k8stone at apple.com <mailto:k8stone at apple.com>
>  Xcode Low Level Tools
> 
> _______________________________________________
> swift-users mailing list
> 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/20160701/e5495bf0/attachment.html>


More information about the swift-users mailing list