Operators

Built-In Operators | Operator Methods

 

Built-In Operators

 Symbol  Name Types Behavior
+ Plus numerical 3 + 4 → 7
- Minus numerical 5 - 4 → 1
* Times numerical 3 * 4 → 12
/ Divided By numerical 99.0 / 100.0 → 0.99
99 / 100 → 0  (integer division only keeps the whole part of the result).
% Mod (modulo) numerical
^ Power numerical 2 ^ 8 → 256
++i
i++
Increment numerical i++ or ++i → i = i + 1
Increment must be a stand-alone statement in Slag.
--i
i--
Decrement numerical i-- or --i → i = i - 1
Decrement must be a stand-alone statement in Slag.
& Bitwise AND integer, logical 0b0011 & 0b0101 → 0b0001
| Bitwise OR integer, logical 0b0011 | 0b0101 → 0b0111
~ Bitwise XOR integer, logical 0b0011 ~ 0b0101 → 0b0100
! Bitwise Complement integer, logical !0b00110101 → 0b11001010
left_shifted() Left Shift integer a.left_shifted(b)
(0b10100011).left_shifted(2) → 0b1010001100
Zeros are shifted into the right-side bit.
left_shifted()
etc. are actual operators, not method calls.
right_shifted() Right Shift integer (0b10100011).right_shifted(2) → 0b101000
Zeros are shifted into the left-side sign bit.
right_xshifted() Right Shift with Sign Extend integer This is like right_shifted() except that the sign bit is replicated with each shift.
left_rotated() Left Rotate integer With each rotation the bits are left-shifted and the leftmost bit (of 32 or 64 bits) is placed into the rightmost bit.
right_rotated() Right Rotate integer With each rotation the bits are right-shifted and the rightmost bit is placed into the leftmost bit (of 32 or 64 bits).
== Is Equal primitives

Results in a Logical true or false result.
3 == 4 → false
3 + 1 == 4 → true

!= Is Not Equal primitives 3 != 4 → true

<
<=
>
>=

Less Than
LT or Eq To
Greater Than
GT or Eq To
numerical 3 < 4 → true
3 <= 4 → true
3 > 4 → false
3 >= 4 → false
<> Compare numerical a <> b → gt, eq, or lt
Underneath: a <> b → Logical(b - a)
3 <> 5 → lt (void)
3 <> 3 → eq (false)
5 <> 3 → gt (true)
is Is reference a is b → true only if a is referencing the same object as b.
isNot Is Not reference a isNot b → true only if a and b reference different objects.
instanceOf Instance Of reference a instanceOf b → true only a and b are the same type or if b's type is an ancestor of a's type.
notInstanceOf Not Instance Of reference a notInstanceOf b → not (a instanceOf b)
as As reference a as SomeType → SomeType reference if (a instanceOf SomeType) or null if (a notInstanceOf SomeType).
and Logical AND logical

false and false → false
false and true → false
true and true → true

or Logical OR logical false or false → false
false or true → true
true or true → true
(5 >= 1) and (5 <= 10) → true
xor Logical XOR logical false xor false → false
false xor true → true
true xor true → false
not Logical NOT logical not true → false
not false → true
not (3 == 4) → true
? Logicalize all types The Postfix Logicalize operator results in true if its operand is non-zero, non-null, or true depending on what type the operand is.
integer_value? → (integer_value != 0)
real_value? → (real_value != 0.0)
reference? → (reference_value isNot null)
logical_value? → (logical_value == true)
upTo
..
Up To numerical

a upTo b
a upTo b step s
a..b
a..b step s
The upTo operator creates a Range iterator object that returns values between a and b with an optional step size (default: 1).
If the step size is negative then the range will start at b and count down towards a.
See also: forEach

upToLessThan
..<
Up To Less Than numerical a upToLessThan b [step s]
a..<b [step s]
Returns a Range iterator that returns values starting at a and up to but not including b with an optional step size.
downTo Down To numerical a downTo b [step s]
Returns a range iterator that counts down from a to b.  The step size is assumed to be (-1) unless otherwise specified.
downToGreaterThan
..>
Down To Greater Than numerical a downToGreaterThan b [step s]
a..>b [step s]
Returns a range iterator that counts down starting at a and down to but not including b.  The step size is assumed to be (-1) unless otherwise specified.
. Member reference obj.m → Accesses member m of object obj.
v.(CastToType)
PrimitiveType(v)
Cast all Primitives: coerces value v to be the given type.
References: forces reference v to be treated as type CastToType.
Int32(3.9) → 3
local Object obj = "Hi"; println( obj.(String).count )  → 2
duplicate(v) Duplicate primitive duplicate(a) → a
No effect with by-value primitives; implemented as a primitive operator to be compatible with the duplicate() operator method.

 

Operator Methods

These may be defined in your classes to allow the given convenience behavior.

 Usage Method Signature Behavior
== op==( DataType other ).Logical obj1 == obj2 → obj1.equals(obj2)
!=   obj != obj2 → not obj1.equals(obj2)
> op<>( DataType other ).Logical obj1 > obj2 → obj1.op<>(obj2) == gt
<   obj1 < obj2 → obj1.op<>(obj2) == lt
 >=   obj1 >= obj2 → obj1.op<>(obj2) != lt
 <=   obj1 <= obj2 → obj1.op<>(obj2) != gt
 <>   obj1 <> obj2 → obj1.op<>(obj2)
 + op+( DataType other ).ResultType obj1 + obj2 → obj1.op+(obj2)
 - op-( DataType other ).ResultType obj1 - obj2 → obj1.op-(obj2)
* op*( DataType other ).ResultType obj1 * obj2 → obj1.op*(obj2)
/ op/( DataType other ).ResultType obj1 / obj2 → obj1.op/(obj2)
% op%( DataType other ).ResultType obj1 % obj2 → obj1.op%(obj2)
^ op^( DataType other ).ResultType obj1 ^ obj2 → obj1.op^(obj2)
& op&( DataType other ).ResultType obj1 & obj2 → obj1.op&(obj2)
| op|( DataType other ).ResultType obj1 | obj2 → obj1.op|(obj2)
~ op~( DataType other ).ResultType obj1 ~ obj2 → obj1.op~(obj2)
! op!().ResultType !obj → obj.op!()
- (unary) op-().ResultType -obj → obj.op-()
PrimType(obj) to_PrimType.PrimType Int32(obj) → obj.to_Int32()
Called when an object is implicitly or explicitly cast to a primitive type.
p.(DataType) DataType.create_from( [p.type] n ) .DataType (5.3).(String) → String.create_from( 5.3 )
5 + "slag" → String.create_from(5).op+("slag")
Called when a primitive type is implicitly or explicitly cast to an object.
obj[index] get( [index.type] index ).ResultType obj[index] → obj.get(index)
Works with any number of "index" parameters, e.g. obj[3,5].
obj[index] = value set([index.type] index,[value.type] value) obj[index] = value → obj.set(index,value)