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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With