The 3 dots Sub querry

Post your Gambas programming questions here.
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

The 3 dots Sub querry

Post by cogier »

Hi,

Can anybody tell me what the 3 dots do as the help example is not very helpful! The text below is taken from here. Passing extra arguments to another function

SINCE 3.6

The ... keyword can be used to transmit the extra arguments to another function accepting them.
Example
Sub PrintMessage(sType As String, sFormat As String, ...)

  ' Do some stuff

End

Sub PrintWarning(sFormat As String, ...)

  PrintMessage("warning", sFormat, ...)

End
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: The 3 dots Sub querry

Post by BruceSteers »

Lol , yeah , that example does not seem to actually pass any arguments it just shows how to , as it has already described lol.

I find a lot of that in Gambas help.
The help text seems to say nothing more than what is obvious.

Like Control.Public (sets control to Public) aaah i see, I'd have never guessed ;) lol
(I actually edited that wiki page to be a bit more descriptive)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: The 3 dots Sub querry

Post by stevedee »

cogier wrote: Sunday 13th December 2020 12:39pm Hi,

Can anybody tell me what the 3 dots do as the help example is not very helpful! ...
Here is a simple example...
Public Sub Button1_Click()

  ThreeDots(TextBox1.Text, TextBox2.Text, TextBox3.Text)

End

Public Function ThreeDots(strName As String, strGender As String, ...) As Boolean
  
  Message.Info(strName & " is " & strGender, ...)
  
  
End
...but I'll try to think of a better one after lunch.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: The 3 dots Sub querry

Post by stevedee »

OK, so the 3 dots allow you to pass any data type (I think) so in the example above you could add:-
Public Sub Button2_Click()
  
  ThreeDots(TextBox1.Text, TextBox2.Text, True)
  CanDisplayName(TextBox1.Text, TextBox2.Text, ...)

End

Public Function CanDisplayName(strName As String, blnChoice As Boolean) As Boolean
  
  If blnChoice Then
    Me.text = "OK to display name"
  Endif
  
End

Where 3dot variable is now type boolean.


...still not a great example, but does this help?
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: The 3 dots Sub querry

Post by stevedee »

OK, it gets a bit more interesting when you have potentially an unknown number of arguments at the time that you write a Method for some Class.

In this example, the functions ThreeMoreDots & LoadData can cope with a variable bunch of data. Obviously the Button function in this case is only creating 10 data variables.
Public iarrNumbers As New Integer[100]

Public Sub Button4_Click()
Dim index As Integer

  For index = 0 To 9
    iarrNumbers[index] = index
  Next
  Me.Text = CStr(ThreeMoreDots("Lucky Numbers", iarrNumbers))
  
End


Public Function ThreeMoreDots(strDataSeries As String, ...) As String
  
  Return LoadData(strDataSeries, ...)
  
End

Public Function LoadData(sDataSeries As String, arrData As Integer[]) As String
Dim iSum As Integer
Dim index As Integer
  
  For index = 0 To arrData.Max
    iSum += arrData[index]
  Next
  Return "Sum of data series called;  " & sDataSeries & ": " & CStr(iSum)
      
End
I can't see any other way of referencing the ... data other than using it as an argument to a Sub/Function (hence the LoadData function above).

It would be very nice if:-
intType = TypeOf(...)
worked, but it doesn't.

The ... in Gambas is basically an implementation of "variadic" function that you will find in other languages (https://en.wikipedia.org/wiki/Variadic_function)

Its seems to be generally regarded as a bad idea! This is mainly because the data presented to the method may not be the correct type.
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: The 3 dots Sub querry

Post by cogier »

Hi Steve,

Thanks for the code and explanation, I can see why you say it's a bad idea you could end up with any old data being passed! Code can often be hard to read but this would just add even more confusion.

I can't see why anybody would want to use it, unless someone has a good reason for it that I can't see....?

(I deleted you duplications)
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: The 3 dots Sub querry

Post by stevedee »

It looks to me as if there is something missing in Gambas. Some other languages have built in methods to help with this.

Take a look at this Basic implementation: https://rosettacode.org/wiki/Variadic_function#BASIC
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: The 3 dots Sub querry

Post by BruceSteers »

I dunno , it seems to work.
It looks to me just like a way of saving writing extra unneeded code when you want to pass a load of arguments through one function to another but not really need them in the first function.

If you consider this example...
as the form loads it calls Test() with a load of arguments.
The Test() function only wants the sName argument and it then calls PrintTest() that does want all the args.
Public Sub Form_Open()

  Test("value of one", "1", 1, 1)
  Test("value of one & 1/2", "1.5", 1.5, 1.5)

End

Public Sub Test(sName As String, ...)

  Print "running Test " & sName
  PrintTest(sName, ...)

End

Public Sub PrintTest(sName As String, sVar As String, iVar As Integer, vVar As Variant)
  
  Print "name: "; sName
  Print "text: "; sVar
  Print "integer: "; Str(iVar)
  Print "TypeValue:"; TypeOf(vVar); ": "; Str(vVar)
  
End


So there the function Test()

I have not needed to use
Public Sub Test(sName As String, sVar As String, iVar As Integer, vVar As Variant)

  Print "running Test " & sName
  PrintTest(sName, sVar, iVar, vVar)

End
and pass all the arguments that PrintTest() wants but Test() does not , the ... takes care of it.

As for variable types and using an unknown count of args i guess that's up to the programmer. If you was not using the ... method but filling in the fields like in the lower example then your types would be known and taken care off. and for unknown quantities you would use an array[]
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: The 3 dots Sub querry

Post by BruceSteers »

cogier wrote: Sunday 13th December 2020 12:39pm Hi,

Can anybody tell me what the 3 dots do as the help example is not very helpful!
You were right about the info not being helpful
I found it a bit confusing too.

I thought it needed an update to be clearer.

Now it reads more like this.

Passing extra arguments to another function
Since 3.6

If you pass a number of arguments to function1() and it then passes them to Function2() but does not use them.
The ... keyword can be used to transmit the extra arguments to the function accepting them.

Example
Sub GotWarning()

  ProcessSomething("warning", "format description", "info")

End

Sub ProcessSomething(sType As String, ...)

  Print "Got message type " & sType
  ' Do something with sType

  PrintMessage(sType, ...)

End

Sub PrintMessage(sType As String, sFormat As String, sInfo As String)

  ' Do some stuff with the arguments passed.

End
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: The 3 dots Sub querry

Post by BruceSteers »

stevedee wrote: Sunday 13th December 2020 4:19pm
I can't see any other way of referencing the ... data other than using it as an argument to a Sub/Function (hence the LoadData function above).

It would be very nice if:-
intType = TypeOf(...)
worked, but it doesn't.

The ... in Gambas is basically an implementation of "variadic" function that you will find in other languages (https://en.wikipedia.org/wiki/Variadic_function)

Its seems to be generally regarded as a bad idea! This is mainly because the data presented to the method may not be the correct type.
I don't think it's been designed like that.
It's not something you can address or access in any way it's just a keyword to pass arguments through to another function.
(like the LoadData function).

Cant see how you'd do it in gambas with the way it already uses dots for oop

I tried checking [...].Count but it says "unexpected ..."
....Count is a no no too.

Handy though.
I've passed many an argument through to another that i didn't need to, will keep ... in mind :)
If at first you don't succeed , try doing something differently.
BruceS
Post Reply