Access

Who can see a declaration in Antril - the six access words, access sections, defaults, friends, and the rules for namespaces, interfaces and types nothing derives from.

Last updated 3 min read Edit on GitHub

On this page

Every declaration has an access: the code that may name it. Access is checked when the program compiles; naming something you cannot see is error AN1400 (a type) or AN1401 (a member).

Access Visible from
public Everywhere, including other libraries.
internal The same program or library.
file The same source file.
private The type itself and the types nested in it. In a namespace, the same file.
inherited The type and the types derived from it.
friendly The type and its friends, declared with [FriendOf(T)]. Friendship goes both ways.

Defaults

  • Members of a type are private unless they say otherwise.
  • Types, and functions and constants of a namespace, are internal.
  • A project can change the default for everything that says nothing with access.default in the BUILD chunk of its .queen file, for example access.default: public (see Project files).

The members of an interface, operators and enum members are always public.

Access sections

Inside a type, a label such as public: gives its access to every member after it, up to the next label or the end of the type. Labels can repeat, and a word written on a single member wins over its section:

AntrilRun
import System;

public class Vault
{
	i32 code = 42;              // private: before any label

	public:
		Peek(Vault other) : i32 => other.code;

	inherited:
		i32 Level = 3;

	friendly:
		i32 Shared = 5;

	file:
		i32 Local = 6;
		public i32 Open = 7;    // the word on the member wins
}

public class BigVault : Vault
{
	public:
		Show() : i32 => Level;  // inherited: visible here
}

[FriendOf(Vault)]
public class Auditor
{
	public:
		Check(Vault v) : i32 => v.Shared;   // friendly: Auditor is a friend
}

Main()
{
	Vault v = new Vault();
	Console.WriteLine(v.Peek(v) + new BigVault().Show() + new Auditor().Check(v) + v.Local + v.Open);
}
Terminal
larva run
63

The labels are public:, private:, internal:, inherited:, friendly: and file:. By convention a label goes one tab further in than its type's braces, and its members one tab more, as above.

Sections also work in extension blocks, and members declared by a static loop take the section the loop is in.

Friends

[FriendOf(Other)] on a type makes it and Other friends: each sees the other's friendly members. Friendship works across libraries, and reflection reports it with typeof(A).IsFriendOf(typeof(B)). FriendOf names one type (AN1070), not the type itself (AN1071).

Rules

Error Rule
AN0038 A declaration has one access word: public private class X is an error.
AN1093 In a namespace, a type is public, internal, file or friendly (not private or inherited), and a function or constant is public, internal, file or private (not inherited or friendly).
AN1094 inherited on a member of a type nothing can derive from (a value, a record, an enum, a static or final class): write private.
AN1095 An access word on an interface member.

An accessor of a property can be more restricted than the property: i32 Level { get; private set; }.

Warning

Not yet available: access checks on nested types. A private nested class can be named and created from outside its type.

Was this page helpful?

No address is stored; your user name is, if you are signed in.

Esc