Difference between static enum and static struct
The main difference is that you cannot construct an enum with no cases. Therefore if you're just looking for something to serve as a namespace for some static members, an enum is preferred as you cannot accidently create an instance.
let e = StaticEnum() // error: 'StaticEnum' cannot be constructed because it has no accessible initializers
let s = StaticStruct() // Useless, but legal
Swift constants: Struct or Enum
Both structs and enumerations work. As an example, both
struct PhysicalConstants {
static let speedOfLight = 299_792_458
// ...
}
and
enum PhysicalConstants {
static let speedOfLight = 299_792_458
// ...
}
work and define a static property PhysicalConstants.speedOfLight
.
Re: A struct will be copied every time i use it or not?
Both struct
and enum
are value types so that would apply to enumerations as well. But that is irrelevant here
because you don't have to create a value at all:
Static properties (also called type properties) are properties of the type itself, not of an instance of that type.
Re: What advantages has the choice of a struct or enum?
As mentioned in the linked-to article:
The advantage of using a case-less enumeration is that it can't accidentally be instantiated and works as a pure namespace.
So for a structure,
let foo = PhysicalConstants()
creates a (useless) value of type PhysicalConstants
, but
for a case-less enumeration it fails to compile:
let foo = PhysicalConstants()
// error: 'PhysicalConstants' cannot be constructed because it has no accessible initializers
C++ declaring static enum vs enum in a class
static
cannot be applied to enum
declarations, so your code is invalid.
From N3337, §7.1.1/5 [dcl.stc]
The
static
specifier can be applied only to names of variables and functions and to anonymous unions ...
An enum
declaration is none of those.
You can create an instance of the enum
and make that static
if you want.
class Example
{
enum Items{ desk = 0, chair, monitor };
static Items items; // this is legal
};
In this case items
is just like any other static data member.
This is an MSVC bug; from the linked bug report it seems the compiler will allow both static
and register
storage specifiers on enum
declarations. The bug has been closed as fixed, so maybe the fix will be available in VS2015.
Struct , class or enum for service object with static methods?
There are two key difference between a class and a struct in Swift. A class allows for inheritance and a class is a reference type while a struct is a value type.
Make your decision based on those few differences.
Since everything in Linker
is static, the difference between reference and value becomes irrelevant since you won't have any instances.
So that leaves inheritance. Will you ever subclass Linker
? If not, use a struct. If yes, use a class.
And now that you are asking about enum, you can probably rule that out over struct since Linker
doesn't appear to have any constants, just methods.
What does static enum mean in C++?
That exact code, with just the ellipsis removed, is not valid C++. You can't use the static
storage class specifier in an enum
declaration; it doesn't make any sense there (only objects, functions, and anonymous unions can be declared static
).
You can, however, declare an enum
and a variable all in one declaration:
static enum Response {
NO_ERROR = 0,
MISSING_DESCRIPTOR
} x;
The static
here applies to x
and it is effectively the same as if you said:
enum Response {
NO_ERROR = 0,
MISSING_DESCRIPTOR
};
static Response x;
What does static imply on constant within an enum or struct
It means myVariable
is a type property – a single property that all instances of the type can use. Essentially a global variable associated with MyEnum
.
In the case of constants declared with let
, this is a way of declaring constants that are scoped to the type that don’t take up space within each instance of that type, i.e.:
struct MySlimStruct {
static let myVariable = "some value"
}
sizeof(MySlimStruct) // returns 0
struct MyFatStruct {
let myVariable = "some value"
}
sizeof(MyFatStruct) // returns 24
In the case of static variables declared with var
, this is a good way of introducing undiagnosable bugs and crashes into your program.
Confused by the usage of a class or a struct as container for static variables (Swift)
What you're doing is clumping global constants into a namespace.
Is there a special Container Object in Swift designed for such purposes?
Yes, a caseless enum is the conventional way to do this, as it is the most lightweight and cannot be accidentally instantiated; it is "pure" namespace.
If you watch some Apple videos you'll see that's how they do it. Personally I used to recommend a struct but I've switched to using enums for the reason given.
Related Topics
No Designated Init for Skshapenode(Circleofradius: Radius)
Swiftui Behavior of .Frame(Height: Nil)
Cannot Convert Value of Type 'Binding<String>' to Expected Argument Type 'Binding<String>'
Tapping an Mkmapview in Swiftui
Swift Spritekit Arc for Dummies
Displaying State of an Async API Call in Swiftui
Why Do We Need to Set Delegate to Self? Why Isn't It Defaulted by the Compiler
What's the Advantage of #Selector Update in Swift 2.3
How to Access Modifiers of a View in Swiftui
Swift 2: Invalid Conversion from Throwing Function of Type to Non-Throwing Function
How to Add Openssl to a Swift Project
Cannot Use Tabview on Swiftui, Watchos
If-Let Any to Rawrepresentable<String>
Why Does Somestruct() Is Anyobject Return True
Swift, for Some Uiviews to Their Overall Controller When Clicked
How to Resolve Error in Unit Testing When We Have Date Comparison in Codable