Wiki
Version 5 (modified by hopscc, 12 years ago)

Update

Access Modifiers

Access Modifiers can be specified on Classes, class (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.

Method Access modifiers

virtual, public is default for methods

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

Valid 'is' keywords are

  • fake - specifies item has no implementation - a placeholder (deprecated in favor of extern)
  • shared - Specifies 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 a template only, subclasses must define fully
  • extern - item has no implementation and is defined elsewhere (outside a cobra file)
  • 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

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 class/method api without implementation

e.g

class TextWriter is extern

    get newLine as String
        pass

  • is shared::
    • on instance variable 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, instance 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::
    • method is redefinable/subclassable (default)
  • is nonvirtual::
    • method is not redefinable, cannot be subclassed.
  • is override::
    • on a method
    • Specifies that this method implementation is a redefinition of an existing(shadowed virtual or abstract) method defined in a superclass
  • is new::
    • on a method
    • designate that this method is a new one that hides the one that would have been inherited from a baseclass. e.g
      class Vehicle
      
          def drive is nonvirtual
              pass
      
      class Car inherits Vehicle
      
          def drive is new  # replaces superclass method
             pass
      

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, attrinutes 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