8.6 Encapsulation
Encapsulation is one of the pillars of object-oriented programming, in which programmers can restrict external access to some elements to protect data integrity. Although structs are not classes, they support encapsulation to a certain degree via private properties and functions.
By default, all properties and functions of a struct are public; this means that any caller can read them and change them if they are mutable. That’s how all former examples in this chapter operated.
In special cases, though, you can mark some struct components as private, which makes those properties inaccessible and invisible to the outer world of callers. In many cases, this makes complex structs safe and simple, protecting their inner mechanisms ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access