.. default-domain:: chpl

.. module:: Reflection
   :synopsis: Functions for finding out about fields, functions, and methods.

Reflection
==========


Functions for finding out about fields, functions, and methods.

.. note ::

  There are several ways in which this module could be improved upon:
  
    * the methods here might be better as type methods,
      so you could use `R.numFields()` instead of `numFields(R)`.
    * :proc:`getField` does not yet return a mutable value.

.. function:: proc numFields(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.
   

.. function:: proc getFieldName(type t, param i: int) param: string

   Get the name of the ith field in a class or record.
   Causes a compilation error if `i` is not in 1..numFields(t).
   
   :arg t: a class or record type
   :arg i: which field to get the name of
   :returns: the name of the field, as a param string
   

.. function:: proc getField(x: ?t, param i: int)

   Get the ith field in a class or record.
   Causes a compilation error if `i` is not in 1..numFields(t).
   
   :arg x: a class or record
   :arg i: which field to get
   :returns: an rvalue referring to that field.
   

.. function:: proc getField(x: ?t, param s: string)

   Get a field in a class or record by name.
   Will generate a compilation error if a field with that name
   is not found.
   
   :arg x: a class or record
   :arg s: the name of a field
   :returns: an rvalue referring to that field.
   

.. function:: proc getFieldIndex(type t, param s: string) param: int

   Get a field index in a class or record, or 0 if
   the field is not found.
   
   :arg t: a class or record type
   :arg s: the name of a field
   :returns: an index `i` usable in getField, or 0 if the field was not found.
   

.. function:: proc hasField(type t, param s: string) param: bool

   Returns `true` if a class or record has a field named `s`,
   or `false` otherwise.
   
   :arg t: a class or record type
   :arg s: the name of a field
   :returns: `true` if the field is present.
   

.. function:: proc canResolve(param fname: string) param: bool

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

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

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

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

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

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

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

