Declarations
Access: public, protected, private
Limit who may use a member - everyone, the class and its subclasses, or the class alone - enforced at compile time.
since 0.1.0-alpha.1linuxwindowswasm
Description
Every member of a class has one of three access levels:
| level | written | who may use the member |
|---|---|---|
| public | public, or no marker |
any code |
| protected | protected |
the declaring class and every class derived from it |
| private | private |
the declaring class only |
A marker is written before a member (protected Ptr handle;) or as a section (protected:) that
applies to every following member up to the next section marker. A member with no marker is public.
The compiler checks every use of a member: a field read or write, a method call, an accessor, an operator, a constructor call, a method reference and a static member. A use the level does not allow is a compile error. The check never changes which member a name refers to, and it costs nothing at run time.
"Inside a class" means its methods, constructors, accessors, operators, field initializers and static members, and the lambdas written in them. A lambda keeps its class's access wherever it is later called. Code that a rule injects into a class has that class's access.
private and protected
use io::console;
class Animal {
private:
int legs = 4;
protected:
string sound = "...";
public:
int legCount() => legs;
string speak() => sound;
}
class Dog : Animal {
new Dog() { sound = "woof"; } // protected: visible in a subclass
}
Dog d = Dog();
console.writeln(d.speak());
console.writeln(d.legCount());
woof
4
error: 'legs' is private in 'Animal'; it is accessible only inside 'Animal'
note: make it public or protected, or reach it through a method of 'Animal'
error: 'sound' is protected in 'Animal'; it is accessible only inside 'Animal' and classes derived from it
note: make it public, or reach it through a method of 'Animal'
The family rule
Inside any class derived from B (or inside B), a protected member of B may be used through
any receiver whose type has that member, not only through this. Sibling subclasses can therefore
use each other's protected members, which a binding layer needs: each wrapper holds its native
handle in a protected field of a shared base, and passes another wrapper's handle to the
underlying call. Code outside the family cannot read the handle.
Sibling wrappers share a protected handle
use io::console;
class ObjectRef {
protected:
int handle = 0;
}
class Widget : ObjectRef {
new Widget(int h) { handle = h; }
}
class Window : ObjectRef {
int child = 0;
void setChild(Widget w) { child = w.handle; } // a sibling's protected member
}
Window win = Window();
win.setChild(Widget(42));
console.writeln(win.child);
42
A protected method that a subclass overrides still belongs to the family of the class that first declared it, so a sibling may call the override too.
Rules
protectedis allowed only on class members. On a struct member it is a compile error (a struct has no derived types), and so it is on a top-level or namespace declaration.- Interface members are always public.
privateorprotectedon an interface member is a compile error, and a class member that implements an interface member must be public. - An override may widen access (protected to public) but not narrow it. A private member cannot be overridden or redeclared in a derived class; make it protected to let subclasses override it.
- A
getorsetaccessor may not be more visible than the field it views. A read uses theget's level when there is one, otherwise the field's; a write uses theset's level, otherwise the field's. "Anyone may read, only the family may write" is a public field with a protectedset. - A private constructor is callable only inside its class, for example from a static factory. A
protected constructor is also callable from a derived class's constructor as
Base::Base(args), which gives a base class that only subclasses can construct. Both rules apply to an explicit call, a typed initializer, a declaration with no initializer (Foo f;) and injection. - Memberwise construction (
P(1, 2)on a class with no constructor) assigns fields in order, so each field it assigns must be accessible where the construction is written. - A bare name inside a class that refers to an inherited member the class cannot access is a compile
error naming that member. It never falls through to a global of the same name, so adding
privateto a base never changes which variable a subclass reads. - A method reference (
C::m,obj.m) and a base-qualified access (this.Base::m) are checked like the call they stand for. Aforloop, ausingstatement and the>>operator check the method they call. - Through multiple inheritance, each member keeps the level its own declaration gives it.
- Reflection is not subject to access:
meta::Fieldandmeta::Methodlist every member and report its level asisPublic,isProtectedandisPrivate.
error: constructor 'Shape' is protected; it is callable only inside 'Shape' and as 'Shape::Shape(...)' from a derived constructor
error: 'area' narrows access from public in 'Shape' to protected in 'Square'; an override may widen access but not narrow it
error: 'get v' is public but the field 'v' it views is private; an accessor cannot be more visible than its field
error: memberwise construction of 'P' assigns private field 'x'; declare a constructor
Examples
A base only subclasses construct, and a factory
use io::console;
class Shape {
protected:
string name = "";
new Shape(string n) { name = n; }
public:
string describe() => name;
}
class Square : Shape {
new Square() { Shape::Shape("square"); }
}
class Token {
private:
int id = 0;
new Token(int i) { id = i; }
public:
static Token issue(int i) => Token(i);
int value() => id;
}
console.writeln(Square().describe());
console.writeln(Token::issue(7).value());
square
7
Public read, protected write
use io::console;
class Account {
int balance = 10;
protected set balance(int v) balance = v;
void deposit(int n) { balance = balance + n; }
}
class Savings : Account {
void addInterest() { balance = balance * 2; }
}
Savings s = Savings();
s.deposit(5);
s.addInterest();
console.writeln(s.balance);
30
Notes
The check happens in the compiler only. Every engine runs the same program it would run without the markers.
See also
- Classes — Declare reference types with fields, methods, constructors and accessors, with generics and inheritance from one or more base classes.
- Value structs — Declare value types that are copied on every assignment, pass and return, with derived equality and explicit mutating methods.
- Interfaces — Declare the members a class must provide, including fields, and let any class satisfy them by declaring those members.
- override — Mark a member that must replace an inherited member or satisfy an interface requirement, and the warning for a member that almost does.
- Accessors — Replace how a field is read or written with get and set, and define computed values and indexers.
- Constructors — Declare constructors with new, select among them by label and argument types, call base constructors, and construct classes that declare none.
- Multiple inheritance — Combine several base classes, how same-named fields and methods collapse or stay separate, and how to reach one base's member by qualification.
- Static members — Class-level methods and fields, reached with '::' and shared by every instance.
- Keywords — The reserved words, the contextual words that are ordinary identifiers elsewhere, and the keywords that may still name members.