Operator
Declares or defines an overloaded operator.
Syntax
`
{ Type | Class | Union | Enum } typename
Declare Operator Cast () As datatype
Declare Operator assignment_op ( [ ByRef | ByVal ] rhs As datatype )
Declare Operator New ( size As UInteger ) As Any Ptr
Declare Static Operator New[] ( size As UInteger ) As Any Ptr
Declare Operator Delete ( buf As Any Ptr )
Declare Static Operator Delete[] ( buf As Any Ptr )
End { Type | Class | Union | Enum }
{ Type | Class | Union } typename
Declare Operator For ()
Declare Operator For ( [ ByRef | ByVal ] stp As typename )
Declare Operator Step ()
Declare Operator Step ( [ ByRef | ByVal ] stp As typename )
Declare Operator Next ( [ ByRef | ByVal ] cond As typename ) As Integer
Declare Operator Next ( [ ByRef | ByVal ] cond As typename, [ ByRef | ByVal ] stp As typename ) As Integer
End { Type | Class | Union }
Declare Operator unary_op ( [ ByRef | ByVal ] rhs As datatype ) As datatype
Declare Operator binary_op ( [ ByRef | ByVal ] lhs As datatype, [ ByRef | ByVal ] rhs As datatype ) As datatype
Operator typename.Cast () As datatype
Operator typename.assignment_op ( [ ByRef | ByVal ] rhs As datatype )
Operator unary_op ( [ ByRef | ByVal ] rhs As datatype ) As datatype
Operator binary_op ( [ ByRef | ByVal ] lhs As datatype, [ ByRef | ByVal ] rhs As datatype ) As datatype
Operator typename.New ( size as uinteger ) As Any Ptr
Static Operator typename.New[] ( size As UInteger ) As Any Ptr
Operator typename.Delete ( buf As Any Ptr )
Static Operator typename.Delete[] ( buf As Any Ptr )
`
Parameters
`typename`
Name of the `Type`, `Class`, `Union`, or `Enum`.
`assignment_op`
`let += -= *= /= \= mod= shl= shr= and= or= xor= imp= eqv= ^=`
`unary_op`
`- not @ * -> abs sgn fix frac int exp log sin asin cos acos tan atn`
`binary_op`
`+ - * / \ mod shl shr and or xor imp eqv ^ = <> < > <= >= andalso orelse`
Description
The built in operators like `=`, `+`, and `cast` have predefined behaviors when used in expressions. These operators can be overloaded to do something other than predefined operations when at least one of the arguments to the operator is a `Type`, `Class`, `Enum`, or `Union` data type.
Operators are just functions. The operator '+' has functionality like `Function Plus( A as DataType, B as DataType ) as DataType`. See Operator Overloading for more information. Operators can be overloaded to accept different data types as parameters. Only the `Cast` Operator can be overloaded to return different types.
Non-static operator members are declared inside the `Type` or `Class`. Global operators are declared outside. All operator definitions (procedure bodies) must appear outside.
`Let`, `Cast`, and other assignment operators must be declared inside the `Type` or `Class`. They are passed a hidden `This` parameter and have a return data type same as the `Type` or `Class` they are declared in.
Unary operators must be declared outside the `Type`, `Class`, or `Enum` and have a return data type explicitly declared. Unary operators can be overloaded to return any valid data type, except for `Operator -> (Pointer To Member Access)` which must return a `Type` or `Class` data type.
Binary operators must be declared outside the `Type`, `Class`, or `Enum` and have a return data type explicitly declared. Binary operators can be overloaded with valid data types, except for relational operators, which must return `Integer`.
`Let` refers to the assignment operator, as in `LET a=b`. The `Let` keyword is omitted in common practice, and is not allowed in the -lang fb dialect. However, `Let()` can be used to assign the fields of a UDT to multiple variables.
See `For`, `Step`, and `Next` for more information on overloading the `For..Next` statement for use with user defined types.
Example
`
Type Vector2D
As Single x, y
'' Return a string containing the vector data.
Declare Operator Cast() As String
End Type
'' Allow two vectors to be able to be added together.
Declare Operator + ( ByRef lhs As Vector2D, ByRef rhs As Vector2D ) As Vector2D
Operator Vector2D.cast () As String
Return "(" + Str(x) + ", " + Str(y) + ")"
End Operator
Operator + ( ByRef lhs As Vector2D, ByRef rhs As Vector2D ) As Vector2D
Return Type<Vector2D>( lhs.x + rhs.x, lhs.y + rhs.y )
End Operator
Dim a As Vector2D = Type<Vector2D>( 1.2, 3.4 )
Dim b As Vector2D = Type<Vector2D>( 8.9, 6.7 )
Print "a = "; a
Print "b = "; b
Print "a + b = "; a + b
`
Dialect Differences

- Only available in the -lang fb dialect.

See also

- `Class`
- `Enum`
- `Type`