ooRexx logo number.cls
#!/usr/bin/env rexx
/* ---------------------------------------------------------------- */
/* Number class, a subclass of the String class                     */
/* Values can be any valid number.                                  */
/*                                                                  */
/* All arithmetic methods need to be defined here, because          */
/* the result of the operation should be another object of          */
/* the Number class, and the result of the String class operators   */
/* always is a String class object.                                 */
/*                                                                  */
/* Note that subclasses of the Number class will yield results      */
/* that belong to that particular receiver subclass.                */
/*                                                                  */
/* NOTE: Requieres oorexx 4.0 or later                              */
/* ---------------------------------------------------------------- */
/*                                                                  */
/* Originally by Ruurd J. Idenburg                                  */
/*                                                                  */
/* No copyright, no licence, no guarantees or warrantees, be it     */
/* explicit, implicit or whatever. Usage is totally and completely  */
/* at the users own risk, the author shall not be liable for any    */
/* damages whatsoever, for any reason whatsoever.                   */
/*                                                                  */
/* Please keep this comment block intact when modifying this code   */
/* and add a note with date and a description.                      */
/*                                                                  */
/* ---------------------------------------------------------------- */
/* 2020/03/05 - Initial version approximately (RJI)                 */
/* 2022/02/14 - Support prefix + and - using forward to super (RJI) */
/* 2022/02/15 - 'UNKNOWN method to handle all .number methods (RJI) */
/* ---------------------------------------------------------------- */

::class number public subclass string

::attribute methods class private
::attribute calculationResult private

::method activate class
-- all (hopefully) String methods that should result in .number instance
  self~methods = ('+','-','/','*','%','//','**', -
-- remove them from method lookup chain, setting method objects to .nil
  unknownMethods = .StringTable~new
  do method over self~methods
    unknownMethods[method] = .nil
-- make the methods unavailable so we can trap them in our UNKNOWN method

::method init
        if (self)~datatype\='NUM' then do          -- if self is not a number
                raise syntax 93.904 array(1,self)  -- then raise an error and quit     

::method unknown
  use arg method, args
-- if not a .String instance method return the nil object (.nil)
  if ''~instanceMethod(method)~isNil then return .nil
-- pass the method and arguments to the superclass and wait for the result
  forward class(super) message (method) arguments (args) continue
-- keep the result
  self~calculationResult = result
-- if not an arithmetic operator/method return the result .String object
  if \self~class~methods~allItems~hasItem(method) then do
    return self~calculationResult
-- create an new .number instance as the result of the arithmetic operation
  return self~class~new(self~calculationResult)

If you feel inclined to make corrections, suggestions etc., please mail me any.
All content © Ruurd Idenburg, 2007–2022, except where marked otherwise. All rights reserved. This page is primarily for non-commercial use only. The Idenburg website records no personal information and sets no ‘cookies’. This site is hosted on a VPS(Virtual Private System) rented from Transip.nl, a Dutch company, falling under Dutch (privacy) laws (I think).

This page updated on Fri, 07 May 2021 12:15:43 +0200 by Ruurd Idenburg.