Operator For (Iteration)
 
Declares or defines operators used by a For...Next loop with user defined type variables

Syntax

{ Type | Class | Union } typename
Declare Operator For ()
Declare Operator For ( [ ByRef | ByVal ] stp As typename )
...
End { Type | Class | Union }

Usage

For iterator [ As typename ] = start_value To end_value [ Step step_value ]
[ ...statements... ]
Next

Parameters

typename
name of the Type, Class, or Union
stp, step_value
a typename object used as an incremental value
iterator
a typename object used as an iterator
end_value
a typename object used as a loop-terminating value
start_value
a typename object used to copy construct or assign to the iterator initially

Description

Operator For, Operator Next and Operator Step can be overloaded in user-defined type definitions to allow objects of that type to be used as iterators and step values in For...Next loops.

Operator For is called immediately after copy constructing or assigning to the iterator object, and allows the object to perform any additional initialization needed in preparation for the loop.

The first version of Operator For is used if no step value is given in the For...Next statement. If a step value is given, the second version is used and is passed the step value.

Example

'' Example Type

Type T

  value As Double

  Declare Constructor( ByVal x As Double = 0 )

  Declare Operator += ( ByVal x As Double )

  Declare Operator For()
  Declare Operator Step()
  Declare Operator Next( ByRef cond As T ) As Integer

  Declare Operator Cast() As String

End Type

Constructor T ( ByVal x As Double )
  value = x
End Constructor

Operator <= ( ByRef lhs As T, ByRef rhs As T ) As Integer
  Operator = ( lhs.value <= rhs.value )
End Operator

Operator T.+= ( ByVal x As Double )
  value +=  x
End Operator

Operator T.for()
End Operator

Operator T.step()
  This += 1
End Operator

Operator T.next( ByRef cond As T ) As Integer
  Operator = ( This <= cond )
End Operator

Operator T.cast() As String
  Operator = Str( value )
End Operator

'' Example Usage

For i As T = 1 To 10
  Print i
Next i

Dialect Differences

  • Only available in the -lang fb dialect.

See also