Structs are value types
suggest changeUnlike classes, which are passed by reference, structures are passed through copying:
var first = "Hello"
var second = first
first += " World!"
// first == "Hello World!"
// second == "Hello"
String is a structure, therefore it’s copied on assignment.
Structures also cannot be compared using identity operator:
window0 === window1 // works because a window is a class instance
"hello" === "hello" // error: argument type 'String' expected to be an instance of a class or class-constrained type
Two struct values have no identity to compare. They are interchangeable when == says they are equal, if the type implements that operator.
These differences from classes are what make structs value types.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents