HOME | 8. Parameterized classes | Try Sather | 10.Immutable classes | Post Messages |
As the methods for operators are pre-defined, you can re-define the operators of your own classes.
Methods | Operators |
---|---|
a.plus(b) | a + b |
a.minus(b) | a - b |
a.times(b) | a * b |
a.div(b) | a / b |
a.pow(b) | a ^ b |
a.mod(b) | a % b |
a.is_lt(b) | a < b |
a.is_eq(b) | a = b |
-a | a.negate |
~a | a.not |
Table 2 shows comparison operators with their corresponding methods.
Operators | Corresponding Methods |
---|---|
a <= b | b.is_lt(a).not |
a >= b | a.is_lt(b).not |
a /= b | a.is_eq(b).not |
a > b | b.is_lt(a) |
Methods | Syntax sugars |
---|---|
b := a.aget(i) | b := a[i] |
a.aset(i, b) | a[i] := b |
The INT class has its aget and aset methods to access the array of bits.
You can modify the program crypt.sa in Abstract Classes
by using this methods.
The integers in the program are XORed by bxor.
It is unnecessary to get XOR's of whole 32 bits because the integers converted from CHAR's,
have only 8 bits.
Following is a more efficient code.
-- replace fs := fs + s.elt!.int.bxor(key_rotate!).char; at line 64 by following code fs := fs + cxor(s.elt!.int,key_rotate!); -- add following method to class XOR private cxor(m,n:INT):CHAR is i,k:INT; loop i:=CHAR::asize.times!; -- 8.times! k:=k.aset(i, m[i].xor(n[i])); end; return k.char; end;
HOME | 8. Parameterized classes | Try Sather | 10.Immutable classes | Post Messages |