Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

public static vs open static vs public class vs open class?

With the introduction of the open keyword in Swift 3 suddenly the following are valid scope modifiers for a method: open static, public static, open class, public class, but what exactly are their differences? I understand that public is meant to be the equivalent of public final in Java allowing open class methods and variables to be overridden, but then what do public class func or open static func mean? Are they synonymous to public static func? i.e. All 3 implementations would not allow overriding by subclasses? Are there unique advantages to each of the 4 different permutations in specific contexts?

like image 229
NoodleOfDeath Avatar asked May 08 '17 18:05

NoodleOfDeath


1 Answers

This question is over-complicated because you're comparing the members of the cartesian product of two variables (open vs public and static vs class), rather than asking about the two variables separately.

It's not a matter of open static vs public static vs open class vs public class, but rather of open vs public and static vs class. They're two orthogonal dimensions.

open vs public

public:

Within the module, the public access specifier allows access and overriding.

From outside the module, the public access specifier allows access, but does not permit overrides/subclasses.

open:

Within the module, the open access specifier allows access and overriding.

From outside the module, the open access specifier allows access, and permits overrides/subclasses.

static vs class

static:

A static member (method or property) is one who is bound to the specific scope (class/struct/enum) it is defined in. It's named such because access to such members is always statically dispatched. This is equivalent to Java's static. Objective C has no equivalent to this.

class:

A class member is one who is bound to a class or its subclasses. class members can be overridden by subclasses. Because of this, they're dynamically dispatched in the general case, although accesses to class members can be devirtualized by the optimizer in some cases. Java has no equivalent to this. This is equivalent to Objective C's class (+) methods.

like image 159
Alexander Avatar answered Oct 22 '22 02:10

Alexander