Member Visibility Levels

Each member of a Class or Record (and, to a limited degree, Interface) has a visibility level that controls which portions of your (or external) code have access to the member.

The following keywords and keyword combinations can be used to introduce a new visibility section that all subsequent members will fall into.

  • private — only accessible from other members of this same type.
  • unit — only accessible from within the same file.
  • unit or protected — only accessible from this type, a subclass or within the same file.
  • unit and protected — only accessible from this type, a subclass that is within the same file.
  • assembly — only accessible from within this project.
  • assembly or protected — only accessible from this type, subclasses or any code within the same project.
  • assembly and protected — only accessible from this type and subclass that are within the same project.
  • protected — only accessible from this type or subclasses.
  • public — accessible from everywhere.
  • published — accessible from everywhere, and excluded from linker optimizations.

All of these visibility levels are available members of Classes or Records. Members of Interfaces are public by default, but under certain conditions, private interface members are permitted, as well.

The unit, assembly and public visibility levels are also available for types themselves.

The published visibility level behaves identical to public, on a conceptual level. On platforms that use the Island compiler back-end, published will exclude the members from linker optimization, making sure they are included in the final executable as part of the type, even when not directly used. Similar behavior can be achieved with the [Published] and [Used] Aspects.

Oxygene has provides two ways to specify this visibility:

Visibility Sections

Traditionally, visibility is defined by the visibility section that the member is declared in. Every occurrence of a visibility specifier from the table below will initiate a new section, and all members that follow with share that visibility. The default visibility is assembly, so any members declared before the first visibility specifier are accessible from the entire project.

type
  MyClass = public class
    method A; // method A and B are "assembly" visible, as that is the default
    method B;
  protected
    method C; // method A and B are "protected"
    method D;
  public
    method E; // method A and B are "public", visible outside of the project
    method F;
  end;

Visibility Modifiers

Alternatively, visibility levels can be specified on individual members, using the visibility specifier as Modifier on the member. A visibility modifier overrides whatever section the member is defined in, and affects the visibility of the individual member only.

This syntax is useful to keep related members close together, regardless of visibility – especially when using the newer Unified Class Syntax.

type
  MyClass = public class
  public
    method A; // method A, B , D, E and F are all "public"
    method B;
    method C; private; // method C is private.
    method D;
    method E; 
    method F;
  end;

See Also