Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!UOGUELPH.BITNET!BOTCHAIR
From: BOTCHAIR@UOGUELPH.BITNET (Alex Bewley)
Newsgroups: comp.lang.modula2
Subject: Defining Operators
Message-ID: 
Date: 29 Sep 89 23:04:26 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Reply-To: Modula2 List 
Organization: The Internet
Lines: 65


    I was struck with an urge to think just the other day and came up with the
  following observation.  I was flipping through some Ada books in the library
  ('cause they outnumber Modula-2 books about 20 to 1) and Ada seems to have a
  feature where you can define an operator for non-standard types.  For example
  suppose you want to add two complex numbers.  You have to:

       Result := AddComplex(A, B);

    Which detracts from structure readability (say what?).  Well, what I mean
  is that, for example, when adding CARDINALs you just have:

       Result := A + B;

    Which (algebraically) reads exactly as you mean it.  In Ada you can
  define the '+' operator to be able to handle the Complex number type.

    Would such a feature be warranted in Modula-2?

    Here is some theoretical code:

(* necessary IMPORTs *)

TYPE
  Complex;     (* abstract type *)

OPERATORTYPE
    "+"  : AddComplex(Complex, Complex) : Complex; (* procedures called *)
    "-"  : SubComplex(Complex, Complex) : Complex; (* when operators used *)
    ":=" : DefineComplex(REAL, REAL) : Complex;
VAR
  Result : Complex;
  A, B   : Complex;

BEGIN
  A := 2.0, 2.0;
  B := 4.0, 4.0;
  Result := A + B;
END;

    The compiler or translator could substitute the necessary procedure
  and make sure that the correct types are being used.
    Thus, the compilable code would look like:

BEGIN
  A := DefineComplex(2.0, 2.0);
  B := DefineComplex(4.0, 4.0);
  Result := AddComplex(A, B);
END;

    This kind of operator definability could be used to simplify reading
  of bit shifting operations.  For example:

    EquipList := (BIOSResult SHL 5) XOR OtherBIOSResult;

    Rather than:

    EquipList := Shl(BIOSResult, 5);
    EquipList := Xor(EquipList, OtherBIOSResult);


    Something to think on...

    Alex
    Just this guy...