logo

abstract class

sys::Obj

   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   2 Dec 05  Brian Frank  Creation
   7  //
   8  
   9  **
  10  ** Obj is the root class of all classes.
  11  **
  12  abstract class Obj
  13  {
  14  
  15  //////////////////////////////////////////////////////////////////////////
  16  // Constructor
  17  //////////////////////////////////////////////////////////////////////////
  18  
  19    **
  20    ** Obj constructor for subclasses.
  21    **
  22    protected new make()
  23  
  24  //////////////////////////////////////////////////////////////////////////
  25  // Virtuals
  26  //////////////////////////////////////////////////////////////////////////
  27  
  28    **
  29    ** Compare this object to the specified for equality.  This method may
  30    ** be accessed via the == and != shortcut operators.  If not overridden
  31    ** the default implementation compares for reference equality using
  32    ** the === operator.  If this method is overridden, then hash() must
  33    ** also be overridden such that any two objects which return true for
  34    ** equals() must return the same value for hash().
  35    **
  36    virtual Bool equals(Obj that)
  37  
  38    **
  39    ** Return -1, 0, or 1 if this object is less than, equal to, or greater
  40    ** than the specified object.  This method may also be accessed via
  41    ** the < <= <=> >= and > shortcut operators.  If not overridden the
  42    ** default implementation compares the toStr() representations.
  43    **
  44    virtual Int compare(Obj that)
  45  
  46    **
  47    ** Return a unique hashcode for this object.  If a class overrides hash()
  48    ** then it must ensure if equals() returns true for any two objects then
  49    ** they have same hash code.
  50    **
  51    virtual Int hash()
  52  
  53    **
  54    ** Return a string representation of this object.
  55    **
  56    virtual Str toStr()
  57  
  58    **
  59    ** Trap a dynamic call for handling.  Dynamic calls are invoked
  60    ** with the -> shortcut operator:
  61    **   a->x        a.trap("x", [,])
  62    **   a->x = b    a.trap("x", [b])
  63    **   a->x(b)     a.trap("x", [b])
  64    **   a->x(b, c)  a.trap("x", [b, c])
  65    ** The default implementation provided by Obj attempts to use
  66    ** reflection.  If name maps to a method, it is invoked with the
  67    ** specified arguments.  If name maps to a field and args.size
  68    ** is zero, get the field.  If name maps to a field and args.size
  69    ** is one, set the field and return args[0].  Otherwise throw
  70    ** UnknownSlotErr.
  71    **
  72    virtual Obj trap(Str name, Obj[] args)
  73  
  74    **
  75    ** Given a relative Uri, resolve it to a target object using this as
  76    ** the base.  The default implementation returns this if path.isEmpty,
  77    ** otherwise it recursively calls trap(uri.path.first).trapUri(uri.tail).
  78    **
  79    virtual Obj trapUri(Uri uri)
  80  
  81  //////////////////////////////////////////////////////////////////////////
  82  // Type
  83  //////////////////////////////////////////////////////////////////////////
  84  
  85    **
  86    ** Return if this Obj is [immutable]`docLang::Concurrency#immutability`
  87    ** and safe to share between threads.  An instance of a const class
  88    ** will return true.  The result of `List.toImmutable` or `Map.toImmutable`
  89    ** will also return true.  A Func object may or may not be (see
  90    ** `sys::Func` for details).  Other instances are assumed mutable and
  91    ** return false.
  92    **
  93    Bool isImmutable()
  94  
  95    **
  96    ** Get the Type instance used to represent this object's type.
  97    ** This method is never overridden directly.
  98    **
  99    virtual Type type()
 100  
 101  //////////////////////////////////////////////////////////////////////////
 102  // Utils
 103  //////////////////////////////////////////////////////////////////////////
 104  
 105    **
 106    ** Write 'x.toStr' to standard output.
 107    **
 108    static Void echo(Obj x)
 109  
 110  }