Wiki
Version 6 (modified by hopscc, 6 years ago)

--

Access Modifiers

Access Modifiers can be specified on Classes, (instance and class) variables and methods to set their visibility and accessibility
These are also called 'isNames' in the Cobra doc and discussion since they're specified with a leading is keyword.

Valid 'is' keywords are

  • fake - specifies item has no implementation - a placeholder (deprecated in favor of extern)
  • extern - item has no implementation and is defined elsewhere (outside a cobra file) (replaces/preferred to fake)
  • shared - Specifies a Class (static) method or variable,
  • virtual, nonvirtual - on methods, specifies that the method is overridable or not
  • override, new - on methods specifies that a method is overriding or replacing a baseclass method of the same signature
  • public, protected, private, internal - specifies item visibility
  • abstract - specifies the class or method is a template only, subclasses must define fully
  • partial - on class types - specifies that the type is defined across multiple files
  • readonly specifies that a variable or field cannot be modified.
    • It must be initialised as early as possible and cannot have its value modified after.

Separate multiple modifiers with , e.g .... is protected, override

protected is default for methods and class or instance variables with _ prefix on name
private is default for methods and class or instance variables with _ _ (double underscore) prefix on name
public is default for variables without any _ prefix

Class is-names

classes and interfaces are public by default

  • fake - specifies class has no implementation - a placeholder (deprecated in favor of extern)
  • extern - class has no implementation and is defined elsewhere (outside a cobra file) - replaces fake.
  • shared - Specifies that the entire class is made of static methods and (class) variables,
  • public, protected, private, internal - specifies the class visibility (default is public)
  • abstract - specifies the class is a template only, subclasses must define fully
  • partial - specifies that the type is defined across multiple files

Method is-names

methods are virtual, public by default

Methods prefixed with _ on their names are protected by default
Methods prefixed with _ _ (double underscore) on their names are private by default.
Methods with no underscore prefixes are public by default.

  • fake - specifies method has no implementation - a placeholder (deprecated in favor of extern)
  • extern - method has no implementation and is defined elsewhere (outside a cobra file) (use instead of fake)
  • shared - Specifies a class (static) method,
  • virtual, nonvirtual - specifies that the method is overridable or not
  • override, new - specifies that a method is overriding or replacing a baseclass method of the same signature
  • public, protected, private, internal - specifies item visibility
  • abstract - specifies the method is a template only, subclasses must define fully

Type variables or fields is-names

These are virtual, public by default

Type variables prefixed with _ on their names are protected by default
Type variables prefixed with _ _ (double underscore) on their names are private by default.
Type variables with no underscore prefixes are public by default.

  • shared - Specifies a class (static) variable (rather than an instance variable),
  • public, protected, private, internal - specifies item visibility
  • readonly specifies that a variable or field cannot be modified once initialised.
    • It must be initialised as early as possible and cannot have its value modified after.

Item Visibility

Visibility (or accessibility) denotes the access to types or type variables (fields)

  • public - the most permissive access level. There are no restrictions on accessing public members or public types.
  • protected - access is limited to the containing class or types derived from the containing class.
  • private - access is limited to the containing type.
  • internal - access is limited to the current assembly (or packaging entity).

These modifiers cannot always map exactly as specified to backend supported visibility layers so best to treat them as decreasing levels of advisory visbility as given above.

Notes and Examples

  • is extern::
    • temporary for defining a class/method API without implementation

e.g

class TextWriter is extern

    get newLine as String
        pass

  • is shared::
    • on a variable/field of a type makes it a class variable.
    • on method makes it a class method
    • (effect is same as static (in C#, Java))

e.g

class Runner
   def main is shared
      print 'main class for Runner'
  • is public/protected/private/internal::
    • on class, type variable or method(s)
    • adjust visibility (default public)
  • is abstract::
    • on class or method(s)
    • Specifies item as a template for subclasses to provide a concrete implementation of.
  • is virtual::
    • on a method
    • method is redefinable/subclassable (default)
  • is nonvirtual::
    • on a method
    • method is not redefinable, cannot be redefined or overridden.
  • is override::
    • on a method
    • Specifies that this method implementation is a redefinition of an existing(shadowed virtual or abstract) method defined in a base (or ancestor) class
  • is new::
    • on a method
    • designate that this method is a new one that hides the one (nonvirtual) that would have been inherited from a baseclass. e.g
      class Vehicle
      
          def drive is nonvirtual
              pass
      
          def steer # virtual by default
      
      class Car inherits Vehicle
      
          def drive is new  # replaces superclass method
             pass
      
          def steer is override # is override must be specified to indicate planned override of virtual baseclass method
      

When a method, property or indexer has the same name and type signature as a base member, you must specify is override or specify is new or use base. in the implementation (which is an implicit is override)

  • is partial::
    • on class types (class, structures, interfaces)
    • indicates that the type is defined across multiple files. Each piece so defined must be so tagged.
    • Idiomatically the first file compiled should have the full declaration (inheritance, attributes etc) and the remainder have only the access modifies ( including 'partial')
  • is readonly::
    • on variables (local and type)
    • indicates that the variable cannot have its value changed
    • the variable must be assigned to (explicitly initialised) on declaration or (for class type variables) must be initialised in the classes initialiser method.

See also

 C# reference Access Modifiers

 C# Accessibility Levels

 Java Access Modifiers