Root Class Object

  • Type Object is the root class of all other Slag classes.
  • If you don't define a base class then a new class definition implicitly extends "Object".
  • Any aspect reference must therefore be instanceOf type Object and may have Object methods called on it. 

  

Object
class
  • DESCRIPTION
    • The root class of all objects. All classes are implicitly or explicitly extended directly or indirectly from class Object.
  • CLASS_METHODS
    • create_duplicate(Object existing).Object
      • This factory method initiates object duplication. The compiler transforms the duplicate(obj) command into Object.create_duplicate(obj).
      • Returns:
          'null' if 'existing' is null.
          existing.create_duplicate otherwise
        
  • METHODS
    • hash_code().Int32
      • Returns the hash code of this object, which some classes (like HashTable) rely on. This default implementation throws an UnsupportedMethodError.
      • Hash codes are used to quickly test and see if two complex objects are NOT equal. The value returned has no intrinsic meaning, but should conform to the following rules:
      • 1. Two objects that are equivalent should produce the same hash code.
      • 2. If an object's state doesn't change, its hash code shouldn't change either. Internally, this precludes the hash code being based on an object's memory address since Slag doesn't guarantee that an object's address won't change over time.
      • This has the following implications:
      • If you override hash_code you should also override op==() and possibly op<>() and ensure the following contract.
      • Invariant:
      •   if (obj1.hash_code != obj2.hash_code)
            obj1.op==(obj2) == false
            obj1.op<>(obj2) != eq
          endIf
        
          if (obj1.op==(obj2))
            obj1.hash_code == obj2.hash_code
          endIf
        
    • create_duplicate().Object
      • Should create a duplicate of this object such that obj1.op==(obj1.create_duplicate) return true.
      • The command duplicate(obj) is transformed by the compiler into Object.create_duplicate(obj), which then calls this method.
      • For optimal performance (by removing an internal typecast in most situations), have your overridden create_duplicate have the same return type as the class it's defined in. For instance, Strings implementation is 'create_duplicate.String'.
      • This default implementation throws an UnsupportedMethodError.
    • to_String().String
      • Returns:
          A String representation of this object.  Most classes
          should override this method to summarize an object's
          state for convenience in testing and debugging.
        
    • op==(Object other).Logical {multimethod}
      • Returns true if this object is equivalant to another.
      • The compiler converts 'obj1 == obj2' into 'obj1.op==(obj2)' and 'obj1 != obj2' into 'not obj1.op==(obj2)'.
      • Implementations of op==() must maintain the following general contract between op==(), op<>(), and hash_code().
      • Contract:
      •  if (obj1.op==(obj2))
           obj1.op<>(obj2) == eq
           obj1.hash_code == obj2.hash_code
         else
           obj1.op<>(obj2) != eq
         endIf
        
        

        Note that two "equal" objects *must* have the same hash code, but "unequal" objects may or may not have the same hash code.

      • This default implementation returns true if the this object and the 'other' reference are the same object ('this is other').
      • op==() is a multimethod, meaning that if there are versions op==(Alpha a) and op==(Beta b) and they are called with an object of unknown type, the most appropriate version method will be invoked. Make your own op==() definitions multimethods as well to propagate this behavior.
    • op<>(Object other).Logical {multimethod}
      • This method is used for ordering (quantitative) comparisons between this object and another.
      • Returns 'eq' if they're equivalent, 'lt' if this object is "less than" the other, or 'gt' if this object is "greater than" another.
      • The compiler converts:
          'obj1 < obj2' -> 'obj1.op<>(obj2) == lt'
          'obj1 > obj2' -> 'obj1.op<>(obj2) == gt'
          'obj1 <= obj2' -> 'obj1.op<>(obj2) != gt'
          'obj1 >= obj2' -> 'obj1.op<>(obj2) != lt'
        
        

        Implementations of op<>() must maintain the following general contract between op==(), op<>(), and hash_code().

      • Contract:
      •   if (obj1.op<>(obj2) == eq)
            obj1.op==(obj2) == true
            obj1.hash_code == obj2.hash_code
          else
            obj1.op==(obj2) == false
          endIf
        
        

        Note that two "equal" objects *must* have the same hash code, but "unequal" objects may or may not have the same hash code.

      • op<>() is a multimethod, meaning that if there are versions op<>(Alpha a) and op==(Beta b) and they are called with an object of unknown type, the most appropriate version method will be invoked. Make your own op<>() definitions multimethods as well to propagate this behavior.
      • This default implementation throws an UnsupportedMethodError.
    • type_name().String : native
      • Returns the true class name of this object. Implemented by the runtime; you should not override this method. Equivalent to calling "runtime_type.name".
      • Example:
          local Object obj = "Hello World"
          println( obj.type_name ) # prints: String
        
    • runtime_type().RuntimeType : native
      • Part of Slag's introspection model, this method returns an object that models the type (i.e. class) of this object.
      • Example:
          # Create a new object of the same class as an existing object of 
          # unknown type.
          local Object obj1 = ...
          local Object obj2 = obj1.runtime_type.create_instance
        
    • runtime_properties().RuntimeProperties
      • Part of Slag's introspection model, this method returns an object that models the properties of this object.
      • Example:
          # Print out the values of any strings that an object contains.
          local Object obj = ...
          local RuntimeType type_string("String")
          forEach (p in obj.runtime_properties)
            if (p.runtime_type == type_string)
              println( "String property $ contains: $" (p.name,p.as_Object) )
            endIf
          endForEach
        
          # Add one to an object's "x" property if it exists (assumed Int32).
          local var p = obj.runtime_properties["x"]
          if (p?) p.as_Int32 = p.as_Int32 + 1    # or: ++p.as_Int32
        
    • runtime_methods().RuntimeMethods
      • Part of Slag's introspection model, this method returns an object that models the methods of this object. Some methods defined in the program source code may not be present - unreferenced methods are culled out by the compiler. If this is a problem it can be prevented by making the method 'requisite'.
      • Example:
          # Given a message name (e.g. "reset") call an "on" method of the same 
          # name (e.g. "on_reset") if it exists.
          local var m = obj.runtime_methods["on_"+mesg_name]
          if (m?) m.call