logo

class

sys::Func

sys::Obj
  sys::Func
   1  //
   2  // Copyright (c) 2007, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   9 Jul 07  Brian Frank  Split from Method
   7  //
   8  
   9  **
  10  ** Func models an executable subroutine.  Functions are typed by a
  11  ** formal parameter list and return value (or Void if no return).
  12  ** Functions are typically defined as method slots on a type, but
  13  ** may also be defined via functional programming constructs such
  14  ** closures and the '&' operator.
  15  **
  16  ** An immutable function is guaranteed to not capture any
  17  ** state from its thread, and is safe to execute on other threads.
  18  ** The compiler marks functions as immutable based on the following
  19  ** analysis:
  20  **   - static methods are always automatically immutable
  21  **   - instance methods on a const class are immutable
  22  **   - instance methods on a non-const class are never immutable
  23  **   - closures which don't capture any variables from their
  24  **     scope are automatically immutable
  25  **   - curried functions which only capture const variables
  26  **     from their scope are automatically immutable
  27  **
  28  ** See `docLang::Functions` for details.
  29  **
  30  final class Func
  31  {
  32  
  33  //////////////////////////////////////////////////////////////////////////
  34  // Constructor
  35  //////////////////////////////////////////////////////////////////////////
  36  
  37    **
  38    ** Private constructor.
  39    **
  40    private new make()
  41  
  42  //////////////////////////////////////////////////////////////////////////
  43  // Signature
  44  //////////////////////////////////////////////////////////////////////////
  45  
  46    **
  47    ** Type returned by the function or sys::Void if no return value.
  48    **
  49    Type returns()
  50  
  51    **
  52    ** Get the formal parameters of the function.
  53    **
  54    Param[] params()
  55  
  56    **
  57    ** Return the associated method if this function implements a
  58    ** method slot.  Otherwise return 'null'.
  59    **
  60    Method method()
  61  
  62  //////////////////////////////////////////////////////////////////////////
  63  // Reflection
  64  //////////////////////////////////////////////////////////////////////////
  65  
  66    **
  67    ** Dynamically invoke this function with the specified arguments and return
  68    ** the result.  If the function has Void return type, then null is returned.
  69    ** The argument list must match the number and type of required parameters.
  70    ** If this function represents an instance method (not static and not a
  71    ** constructor) then the first argument must be the target object.  If the
  72    ** function supports default parameters, omit arguments to use the defaults.
  73    ** It is permissible to pass more arguments then the number of method
  74    ** parameters - the additional arguments are ignored.  If no arguments are
  75    ** required, you may pass null for args.
  76    **
  77    virtual R call(Obj[] args)
  78  
  79    **
  80    ** Convenience for dynamically invoking an instance method with
  81    ** specified target and arguments.  If this method maps to an
  82    ** instance method, then it is semantically equivalent to
  83    ** 'call([target, args[0], args[1] ...])'.  Throw UnsupportedErr
  84    ** if called on a function which is not an instance method.
  85    **
  86    virtual R callOn(Obj target, Obj[] args)
  87  
  88    **
  89    ** Optimized convenience for call([,]).
  90    **
  91    virtual R call0()
  92  
  93    **
  94    ** Optimized convenience for call([a]).
  95    **
  96    virtual R call1(A a)
  97  
  98    **
  99    ** Optimized convenience for call([a, b]).
 100    **
 101    virtual R call2(A a, B b)
 102  
 103    **
 104    ** Optimized convenience for call([a, b, c]).
 105    **
 106    virtual R call3(A a, B b, C c)
 107  
 108    **
 109    ** Optimized convenience for call([a, b, c, d]).
 110    **
 111    virtual R call4(A a, B b, C c, D d)
 112  
 113    **
 114    ** Optimized convenience for call([a, b, c, d, e]).
 115    **
 116    virtual R call5(A a, B b, C c, D d, E e)
 117  
 118    **
 119    ** Optimized convenience for call([a, b, c, d, e, f]).
 120    **
 121    virtual R call6(A a, B b, C c, D d, E e, F f)
 122  
 123    **
 124    ** Optimized convenience for call([a, b, c, d, e, f, g]).
 125    **
 126    virtual R call7(A a, B b, C c, D d, E e, F f, G g)
 127  
 128    **
 129    ** Optimized convenience for call([a, b, c, d, e, f, g, h]).
 130    **
 131    virtual R call8(A a, B b, C c, D d, E e, F f, G g, H h)
 132  
 133  }