Property access using colon notation

The colon notation accesses named parts (properties) of a value. It is used to get and set fields, call methods, construct compound symbols, and more. Evaluating the form owner:property evaluates the owner then it extracts the named property of the result.

property-access-abbreviation ::= property-owner-expression:property-name
property-owner-expression ::= expression
property-name ::= identifier | ,expression

The property-name is usually a literal name, but it can be an unquoted expression (i.e. following a ,), in which case the name is evaluated at run-time. No separators are allowed on either side of the colon.

The input syntax owner:part is translated by the Scheme reader to the internal representation ($lookup$ owner (quasiquote part)).

Part lookup rules

Evaluation proceeds as follows. First property-owner-expression is evaluated to yield an owner object. Evaluating the property-name yields a part name, which is a simple symbol: Either the literal identifier, or the result of evaluating the property-name expression. If the expression evaluates to a string, it is converted to a symbol, as if using string->symbol.

  • If the owner implements gnu.mapping.HasNamedParts, then the result is that of invoking the get method of the owner with the part name as a parameter.

    As a special case of this rule, if owner is a gnu.mapping.Namespace, then the result is the compound symbol in that namespace.

  • If owner is a java.lang.Class or a gnu.bytecode.ObjectType, the result is the static member named part (i.e. a static field, method, or member class).

  • If owner is a java.lang.Package object, we get the member class or sub-package named part.

  • Otherwise, we look for a named member (instance member or field).

    Note you can’t use colon notation to invoke instance methods of a Class, because it will match a previous rule. For example if you want to invoke the getDeclaredMethod method of the java.util.List , you can’t write (java.util.List:getDeclaredMethod because that will look for a static method in java.util.List. Instead, use the invoke or invoke-sttic method. For example: (invoke java.util.List 'getDeclaredMethod).

If the colon form is on the left-hand-side of an assignment (set!), then the named part is modified as appropriate.

Specific cases

Some of these are deprecated; more compact and readable forms are usually preferred.

Invoking methods

(instance:method-name arg ...)
(class:method-name instance arg ...)
(class:method-name arg ...)
(*:method-name instance arg ...)

For details see Method operations.

Accessing fields

class:field-name
instance:field-name
(prefix:.field-name instance)

For details see Field operations.

Type literal

(type:<>)

Returns the type. Deprecated; usually you can just write:

type

Type cast

Performs a cast. Deprecated; usually you can just write:

->type

Type test

(type:instanceof? expression)

Deprecated; usually you can just write:

(type? expression)

New object construction

(type:new arg ...)

Deprecated; usually you can just write:

(type arg ...)

Getting array length

expression:length
(expression:.length)