Comparison between Properties & Functions overloading behaviour

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Comparison between Properties & Functions overloading behaviour

Post by Lost Zergling »

Code: Select all

Declare Function SUM Overload (A As Integer,B As Integer, MyArrElement as Integer ) As Integer
Declare Function SUM Overload (A As Single,B As Single, MyArrElement as Single ) As Single
Declare Function SUM Overload (A As Single,B As Single, MyArrElement as String ) As String

Function SUM  (A As Integer,B As Integer, MyArrElement As Integer ) As Integer
    Print @MyArrElement & " this one is worse than useless : ByRef is NOT supported !.." & chr(10) &  "             But MyArrElement (Byte) is overloaded "
    Function=A+B+MyArrElement
End Function   
Function SUM  (A As Single,B As Single, MyArrElement  As Single ) As Single
   Function=A+B+MyArrElement
End Function   
Function SUM  (A As Single,B As Single, MyArrElement As String ) As String
    Print @MyArrElement & " Whereas ByRef & ByVal access on string arrays element are supported !"
    Function="" ' MyArrElement
End Function  

Dim As Integer A,B
Dim As Single A1,B1
Dim As Byte array()
Redim array(1)
'Dim array(1)  As Byte

array(0)=2
array(1)=1

A=2
B=3
A1=2.
b1=3.
Print "Function overload test -----------------------"
Print
Print  @array(0) & " = address of array(0)"
Print SUM(A,B, array(0) )
Print SUM (A1,B1, array(0) )

Dim array_2(1) As String
array_2(0)="String value"
Print  @array_2(0)
Print SUM (A1,B1, array_2(0) )

'sleep

Type Test
    Dim toto as uByte
    Public:
    Declare Property Set(ByRef MyArrElement as Integer ) As Integer
    Declare Property Set(ByRef MyArrElement as Single ) As Single
    Declare Property Set(ByRef MyArrElement as String ) As String
End Type

Property Test.Set(ByRef MyArrElement as Integer ) As Integer
    Print @MyArrElement & " this one is usefull : ByRef is supported" & chr(10) &  "             (but MyArrElement (Byte) isn't 'auto-overloaded')."
    Return 1
End Property
Property Test.Set(ByRef MyArrElement as Single ) As Single
    Print @MyArrElement : Return 1
End Property
Property Test.Set(MyArrElement as String ) As String
    Print @MyArrElement : Return ""
End Property

Dim array_3(2) As Integer
array_3(0)=1

Dim MyTest As Test
Print
Print "Property overload test -------------------"
Print
MyTest.Set( array_3(0) ) 

sleep
system
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Comparison between Properties & Functions overloading behaviour

Post by fxm »

To be able to pass a variable by reference to a procedure, the variable must be of the same type as the one declared for the procedure (it's not true for your functions).
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Re: Comparison between Properties & Functions overloading behaviour

Post by Lost Zergling »

Hi fxm,
I had completely missed the point, because of the (pretty nicely permissive) syntax ! thank you
I got it. FUNCTION Overloading is fine even with variable length arrays !
By the way, I can use overloading to build functions that will help me simplify the syntax of the code.
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Re: Comparison between Properties & Functions overloading behaviour

Post by Lost Zergling »

fxm,

Tell me if i'm wrong, but,..
The thread we had about the descriptor was really nonsense !!!?? Or what ? Write access needed ? Infinite number of dimension support ?!!?
FBC 1.05 W32.

Code: Select all

Declare Function SET Overload (MyArray() as Integer ) As Integer
Declare Function SET Overload (MyArray() as String ) As Integer
Declare Function SET Overload (MyArray() as Double ) As Integer

Dim Shared ANY_Ptr As Any Pointer
Dim Shared ArrayMask()  As Integer

Function SET Overload (MyArray() as Integer ) As Integer
    Function=1
End Function  

Function SET Overload (MyArray() as String ) As Integer
    Function=1
End Function  
    
Function SET Overload (MyArray() as Double ) As Integer
    Dim DBL_Ptr As Double Pointer
    Dim As Integer NbDim, NbElements, i    
    NbDim=Ubound(MyArray,0) : Redim ArrayMask(NbDim,1)
    Print "NbDim=" & NbDim
    Select Case As Const NbDim
    Case 1 : ANY_Ptr=@MyArray(Lbound(MyArray,1)) 
    Case 2 : ANY_Ptr=@MyArray(Lbound(MyArray,1), Lbound(MyArray,2))
    Case 3 : ANY_Ptr=@MyArray(Lbound(MyArray,1), Lbound(MyArray,2), Lbound(MyArray,3))
    'And so on
    Case Else : 
    End Select
    NbElements=1 : For i=1 To Ubound(MyArray,0) : NbElements=NbElements*(Ubound(MyArray, i)-LBound(MyArray, i)+1) : ArrayMask(i,0)=LBound(MyArray, i) : ArrayMask(i,1)=UBound(MyArray, i) : Next i
    Print "First element value=" &  Str(*Cptr(Double Ptr, ANY_Ptr))
    Print "Nb Elements=" & NbElements
    Print "Element Size=" & SizeOf(Double)
    ANY_Ptr+=(NbElements-1)*SizeOf(Double)
    Print "Last element value=" & Str(*Cptr(Double Ptr, ANY_Ptr))
    For i=1 to Ubound(ArrayMask,1)
        Print "Dimension " & i & " :  " & ArrayMask(i,0) & " To " & ArrayMask(i,1)
    Next i
    Print "From that point it is really easy to compute a descriptor"
    Function=1
End Function  


Dim  aArray() As Double
ReDim aArray(1,3)

aArray(0,0)=1
aArray(1,3)=2
Set(aArray())

Sleep
System
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Comparison between Properties & Functions overloading behaviour

Post by fxm »

Lost Zergling wrote:The thread we had about the descriptor was really nonsense !!!??
Which thread exactly are you referring to?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Comparison between Properties & Functions overloading behaviour

Post by fxm »

The number of dimensions is limited to 8 for an array.
But infinite number of overload functions because infinite number of datatype (the pre-defined types, plus all possible UDTs) ?
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Re: Comparison between Properties & Functions overloading behaviour

Post by Lost Zergling »

Ha, I NOW understand why only 8 #IF in macros ! Well I knew number of UDT's infinite possibilities or similar consequences !
The reason why I decided to scope my design to FB most common types. A programmer can overload an UDT as needed, this the reason why of a programing language. Shall we not forget the 'user' is also a progammer.. There somewhere a difficulty in doing hard code to automate a feature in a tool (FBC) whose design enable the managment of this feature. Of course, generic feature is far better, but I didn't identify this point as the most critical topic in the context I designed. Well, I should have say really crazy !! I really thought this also.. I was thinking of that thread "New array features" because of the so high level of technicity just around the descriptor. Indeed, very interesting !
Post Reply