Reflection

Usage

use Reflection;

or

import Reflection;

Support for reflecting about properties of a Chapel program’s code.

Functions for reflecting about language elements, such as fields, functions, and methods.

For reflecting about aspects of the compilation process, see ChplConfig.

proc getNumFields(type t) param : int

Return the number of fields in a class or record as a param. The count of fields includes types and param fields.

proc numFields(type t) param : int

Warning

‘numFields’ is deprecated - please use ‘getNumFields’ instead

Return the number of fields in a class or record as a param. The count of fields includes types and param fields.

proc getFieldName(type t, param idx: int) param : string

Get the name of the field at idx in a class or record. Causes a compilation error if idx is not in 0..<getNumFields(t).

Arguments:
  • t – a class or record type

  • idx – which field to get the name of

Returns:

the name of the field, as a param string

proc getField(const ref obj: ?t, param idx: int) param  where idx >= 0 && idx < getNumFields(t) && isParam(__primitive("field by num", obj, idx + 1))

Get the field at idx in a class or record. When the field at idx is a param, this overload will be chosen to return a param. Causes a compilation error if idx is not in 0..<getNumFields(t).

Arguments:
  • obj – a class or record

  • idx – which field to get

Returns:

the param that field represents

proc getField(const ref obj: ?t, param idx: int) type  where idx >= 0 && idx < getNumFields(t) && isType(__primitive("field by num", obj, idx + 1))

Get the field at idx in a class or record. When the field at idx is a type variable, this overload will be chosen to return a type. Causes a compilation error if idx is not in 0..<getNumFields(t).

Arguments:
  • obj – a class or record

  • idx – which field to get

Returns:

the type that field represents

proc getField(const ref obj: ?t, param idx: int) const ref

Get the field at idx in a class or record. Causes a compilation error if idx is not in 0..<getNumFields(t).

Arguments:
  • obj – a class or record

  • idx – which field to get

Returns:

a const reference to that field.

proc getField(const ref obj: ?t, param name: string) param  where getFieldIndex(t, name) != -1 && isParam(getField(obj, getFieldIndex(t, name)))

Get a field in a class or record by name. When the named field is a param, this overload will be chosen to return a param. Will generate a compilation error if a field named name is not found.

Arguments:
  • obj – a class or record

  • name – the name of a field

Returns:

the param that field represents

proc getField(const ref obj: ?t, param name: string) type  where getFieldIndex(t, name) != -1 && isType(getField(obj, getFieldIndex(t, name)))

Get a field in a class or record by name. When the named field is a type variable, this overload will be chosen to return a type. Will generate a compilation error if a field named name is not found.

Arguments:
  • obj – a class or record

  • name – the name of a field

Returns:

the type that field represents

proc getField(const ref obj: ?t, param name: string) const ref

Get a field in a class or record by name. Will generate a compilation error if a field named name is not found.

Arguments:
  • obj – a class or record

  • name – the name of a field

Returns:

a const reference to that field.

proc getFieldRef(ref x: ?t, param i: int) ref

Warning

‘getFieldRef’ is unstable

Get a mutable ref to the ith field in a class or record. Causes a compilation error if i is not in 0..<getNumFields(t) or if the argument is not mutable.

Arguments:
  • x – a class or record

  • i – which field to get

Returns:

a mutable reference to that field.

proc getFieldRef(ref x: ?t, param s: string) ref

Warning

‘getFieldRef’ is unstable

Get a mutable ref to a field in a class or record by name. Will generate a compilation error if a field with that name is not found or if the class or record is not mutable.

Arguments:
  • x – a class or record

  • s – the name of a field

Returns:

a mutable reference to that field.

proc getFieldIndex(type t, param name: string) param : int

Get the index of a field named name in a class or record type t, or -1 if the field name is not found.

Arguments:
  • t – a class or record type

  • name – the name of a field

Returns:

an index usable in getField, or -1 if the field was not found.

proc hasField(type t, param name: string) param : bool

Returns true if a class or record has a field named name, or false otherwise.

Arguments:
  • t – a class or record type

  • name – the name of a field

Returns:

true if the field is present.

proc isFieldBound(type t, param idx: int) param : bool

Warning

‘isFieldBound’ is unstable - consider using ‘T.fieldName != ?’ syntax instead

Returns true if the field at idx has been instantiated in a given class or record type t.

Arguments:
  • t – a class or record type

  • idx – which field to query

Returns:

true if the field is instantiated

proc isFieldBound(type t, param name: string) param : bool

Warning

‘isFieldBound’ is unstable - consider using ‘T.fieldName != ?’ syntax instead

Returns true if the field named name has been instantiated in a given class or record type t.

Arguments:
  • t – a class or record type

  • name – the name of a field

Returns:

true if the field is instantiated

proc canResolve(param fname: string) param : bool

Warning

The ‘canResolve…’ family of procedures are unstable

Returns true if a function named fname taking no arguments could be called in the current scope.

proc canResolve(param fname: string, args ...) param : bool

Warning

The ‘canResolve…’ family of procedures are unstable

Returns true if a function named fname taking the arguments in args could be called in the current scope.

proc canResolveMethod(obj, param fname: string) param : bool

Warning

The ‘canResolve…’ family of procedures are unstable

Returns true if a method named fname taking no arguments could be called on obj in the current scope.

proc canResolveMethod(obj, param fname: string, args ...) param : bool

Warning

The ‘canResolve…’ family of procedures are unstable

Returns true if a method named fname taking the arguments in args could be called on obj in the current scope.

proc canResolveTypeMethod(type t, param fname: string) param : bool

Warning

The ‘canResolve…’ family of procedures are unstable

Returns true if a type method named fname taking no arguments could be called on type t in the current scope.

proc canResolveTypeMethod(type t, param fname: string, args ...) param : bool

Warning

The ‘canResolve…’ family of procedures are unstable

Returns true if a type method named fname taking the arguments in args could be called on type t in the current scope.

proc getLineNumber() param : int

Returns the line number of the call to this function.

proc getFileName() param : string

Returns the file name this function was called from.

proc getRoutineName() param : string

Returns the name of the function this function was called from.

proc getModuleName() param : string

Returns the name of the module this function was called from.