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

Nicholas Outram nicholas.outram at icloud.com
Sun Jul 3 00:53:06 CDT 2016


You are right of course.

 I'm looking at this more from the eyes of an educator, where anything that reduces ambiguity helps. Students naively gravitating towards a singleton pattern is one of the battles I face. Some learners don't even properly understand the difference or risks. 
Invoking a static method can only mutate shared memory, making it a simple candidate for dangerous practise, so it's helpful when a language is explicit. 

Ultimately you are right of course, as you say, instance methods can do this also. 
(Maybe it's also my own personal style / bias as I tend to avoid referencing static variables from instance methods where possible)

Good discussion!




Sent from my iPad

> On 1 Jul 2016, at 19:01, Jens Alfke <jens at mooseyard.com> wrote:
> 
> 
>> On Jul 1, 2016, at 10:28 AM, Nicholas Outram via swift-users <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!
> 
> —Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160703/c034c779/attachment.html>


More information about the swift-users mailing list