About the overloading of the "For" operator:
A default constructor must at least exist (either explicitly, or implicitly) for the [For..Next] operator overloading without step explicitly defined, but not necessary if the step is explicitly defined.
Example of [For..Next] operator overloading without Step operator:
Type UDT Dim As Integer I '' Declare Constructor () Declare Operator For () Declare Operator Step () Declare Operator Next (Byref cond As UDT) As Integer End Type ''Constructor UDT () ''End Constructor Operator UDT.For () End Operator Operator UDT.Step () This.I += 1 End Operator Operator UDT.Next (Byref cond As UDT) As Integer Return (This.I <= Cond.I) End Operator Dim As UDT u1, u2 u1.I = 1 u2.I = 10 For u As UDT = u1 To u2 '' If default constructor is non-existent: error 24: Invalid data types Print u.I Next u Sleep
Example of [For..Next] operator overloading with Step operator:
Type UDT Dim As Integer I '' Declare Constructor () Declare Operator For (Byref stp As UDT) Declare Operator Step (Byref stp As UDT) Declare Operator Next (Byref cond As UDT, Byref stp As UDT) As Integer End Type ''Constructor UDT () ''End Constructor Operator UDT.For (Byref stp As UDT) End Operator Operator UDT.Step (Byref stp As UDT) This.I += stp.I End Operator Operator UDT.Next (Byref cond As UDT, Byref stp As UDT) As Integer If stp.I >= 0 Then Return (This.I <= Cond.I) Else Return (This.I >= Cond.I) End If End Operator Dim As UDT u1, u2, u3 u1.I = 1 u2.I = 20 u3.I = 5 For u As UDT = u1 To u2 Step u3 '' OK even if default constructor is non-existent Print u.I Next u Sleep
Referring to the forum at post:
http://www.freebasic.net/forum/viewtopic.php?p=224201#p224201
For the [For..Next] operator overloading without step explicitly defined, it seems that defining only a conversion constructor can suffice as well.
Last edit: fxm (freebasic.net) 2019-08-08
Note: In the first example, defining a default constructor clears the error on the 'For' line, but this default constructor is not even called by the [For..Next] block!