B4J Question [BANano] Any possibility of function overload like?

Mashiane

Expert
Licensed User
Longtime User
Hi there

In VB there was function overload and I believe java has something like that too, ie defining a function multiple times but with different initialization parameters. Well, this is necessary it, but something interesting, I have just came across.

B4X:
function sum() {
  let result = 0;
  for (const argument of arguments) {
    result += argument;
  }
  return result;
}

sum(5, 8); // 13

Which is valid javacript. I'm curious...

Thanks in advance...
 
Solution
Sorry, I should have used your example:

Variable Arguments, Take Two:
Sub sum(A() As Object) As Object
    
    If A.Length = 0 Then
        Return Null
    Else If A(0) Is Int Then
        Dim IntSum As Int = 0
        For Each I As Int In A
            IntSum = IntSum + I
        Next
        Return IntSum
    else if A(0) Is Float Or A(0) Is Double Then
        Dim DoubleSum As Double = 0
        For Each D As Double In A
            DoubleSum = DoubleSum + D
        Next
        Return DoubleSum
    else if A(0) Is String Then
        Dim StringSum As String = ""
        For Each S As String In A
            StringSum = StringSum & S
        Next
        Return StringSum
    Else
        Return Null
    End If

End Sub


Log(sum(Array As Object(5, 8)))...

emexes

Expert
Licensed User
This might be enough to sate your curiousity:

Variable Arguments (good as):
Sub FunctionWithVariableArguments(A() As Object)
    Log("There are " & A.Length & " arguments")
    
    For I = 0 To A.Length - 1
        Dim ShowAs As String = A(I)
        If A(I) Is Int Then
            ShowAs = "the integer " & ShowAs
        else if A(I) Is String Then
            ShowAs = "the string """ & ShowAs & """"
        End If
        
        Log(I & " = " & ShowAs)
    Next
End Sub

FunctionWithVariableArguments(Array As Object(3, "point", 1, 4, 1, 5, 9, "etc", 2.718))

Log Output:
Waiting for debugger to connect...
Program started.
There are 9 arguments
0 = the integer 3
1 = the string "point"
2 = the integer 1
3 = the integer 4
4 = the integer 1
5 = the integer 5
6 = the integer 9
7 = the string "etc"
8 = 2.718
 
Upvote 0

emexes

Expert
Licensed User
Sorry, I should have used your example:

Variable Arguments, Take Two:
Sub sum(A() As Object) As Object
    
    If A.Length = 0 Then
        Return Null
    Else If A(0) Is Int Then
        Dim IntSum As Int = 0
        For Each I As Int In A
            IntSum = IntSum + I
        Next
        Return IntSum
    else if A(0) Is Float Or A(0) Is Double Then
        Dim DoubleSum As Double = 0
        For Each D As Double In A
            DoubleSum = DoubleSum + D
        Next
        Return DoubleSum
    else if A(0) Is String Then
        Dim StringSum As String = ""
        For Each S As String In A
            StringSum = StringSum & S
        Next
        Return StringSum
    Else
        Return Null
    End If

End Sub


Log(sum(Array As Object(5, 8)))    '13

Log(sum(Array As Object("5", "8")))    '58

Log output:
Waiting for debugger to connect...
Program started.
13
58
 
Upvote 1
Solution

emexes

Expert
Licensed User
That is why many BANano methods also have an Array as parameter.

yes, it'd be nice if declared argument "..." would magically wrap the remaining arguments like "Array As Object( ... )"

on the other hand, that's just yet another special case that risks complicating B4X parsing down the track

vs after you've used "Array As Object" a few times, the verbiage blends into the background and looks "normal"
 
Upvote 0
Top