[Gambas-user] "Groups" of optional arguments

Brian G brian at westwoodsvcs.com
Wed Nov 30 20:32:35 CET 2022





----- On Nov 30, 2022, at 11:17 AM, Brian G brian at westwoodsvcs.com wrote:

> ----- On Nov 30, 2022, at 8:15 AM, T Lee Davidson t.lee.davidson at gmail.com
> wrote:
> 
>> On 11/30/22 03:04, BB wrote:
>>> Maybe I'm overthinking this or maybe my brain is just not as sharp as it used to
>>> be.
>>> 
>>> Short explanation: I think I need to group optional arguments something along
>>> the lines of :
>>> 
>>> Sub XYZ ( arg1 As String, Optional ( arg2 As Boolean, arg3 As Integer = 0),
>>> Optional ( arg4 As Date = Null, arg5 as Integer =
>>> -Inf ) )
>>> 
>>> The illogical thinking is that calls to the method
>>> 
>>>   * must have arg1 provided and
>>>   * either
>>>       o arg2 and possibly arg3
>>>   * or
>>>       o possibly arg4 and possibly arg5
>>> 
>>> The example is simpified, btw. 😁
>>> 
>>> Is there a way to do this?
>>> 
>>> 
>> 
>> This may be a gross misunderstanding of the problem. But, couldn't you pass in
>> the callee's class as a required parameter in
>> addition to the reqion, and pass all the optionals as variadic or, simply,
>> positional optionals?
>> 
>> Ie.: given Sub XYZ ( arg1 As String, Optional ( arg2 As Boolean, arg3 As Integer
>> = 0), Optional ( arg4 As Date = Null, arg5 as
>> Integer = -Inf ) )
>> 
>> data = XYZ(regionid, Object.Type(Me), ...) , or
>> data = XYZ(regionid, Object.Type(Me), Optional arg3 as blah, Optional arg4 as
>> blahblah, etc.)
>> 
>> 
>> and handle the myriad optional arguments based on who's calling? Or would that
>> lead to more spaghetti code?
>> 
>> 
>> --
>> Lee
>> 
>> 
>> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
> 
> How about from your example the following script example of parsing parameters:
> 
> #!/usr/bin/env gbs3
> ' Gambas Script File Created 11/30/2022 17:09:34.817
> 
> class xyz
> 
> Static Public Sub _call(regionid As String, ...)
> 
>    On param.count Goto sublabel1, sublabelerr, sublabel2, sublabelerr, sublabel4 '
>    1 parm, 2 parms, 3 params 4 params
> 
>    Dim a As Integer
>    Dim b As String
>    Dim offset As Integer = 0
>    Dim OutputLine As String = ""
> 
> SubLabel1:
>    GoSub sublabel11
>    Print "editVariety" & Outputline
> 
> sublabel11:  ' one parameter
>    OutputLine = " -r " & regionID
>    'do stuff here
>    Return
> 
> Sublabel2:  ' three parameters
>    GoSub SubLabel11
> 
>    If Not (TypeOf(param[0]) = gb.boolean) Then
>        GoSub SubLabel22
>    Else
>        GoSub Sublabel21
>    Endif
>    Print "editVariety " & outputLine
>    Return
> 
> Sublabel21: ' First parm was boolean
>    ' Print "optional Parm set with boolean as first and ";; param.count;; "params
>    Values =";; param[0];; ",";; param[1];;
>    OutputLine &= " -v " & Str(param[0]) & " -Y " & Str(Param[1])
>    ' do stuff here
>    Return
> 
> SubLabel22: ' First param was not boolean
>    'If Param.count = 2 Then Print "optional Parm set not boolean as first and ";;
>    param.count;; "params Values =";; Else Print ",";;
>    'Print param[0 + offset];; ",";; param[1 + offset]
>    outputline &= " -u " & Str(Param[0 + offset]) & " -q " & Str(Param[1 + offset])
>    ' do stuff here
>    Return
> 
> SublabelErr: ' Invalid parm count
>    Print
>    error.raise("Error : Not enough parameters (" & Str(Param.count) & ")")
> 
> Sublabel4:
>    'Print "Recieved 4 parameters"
>    offset = 2
>    GoSub sublabel11
>    GoSub sublabel21
>    GoSub sublabel22
>    Print "editVariety" & OutPutline
> 
> End
> end class
> 
> 
>    xyz("Sacramento")
>    Try xyz("Boston", True)
>    If Error Then Print "Error", "Boston";; Error.text
>    xyz("Boston", True, "1987")
>    Try xyz("Washinton", "Badcnt")
>    If Error Then Print "Error", "Washinton";; Error.text
>    xyz("Washinton", "Arg3", "Arg4")
>    Try xyz("Daton", True, "2007", "arg3")
>    If Error Then Print "Error", "Daton";; Error.text
>    xyz("Daton", True, "2008", "arg3", "arg4")
> 
> 
> 
> QUIT 0
> catch
> Print error.text&"\n" & error.where
> 
> 
> 
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
Sorry forgot to mention 

Basically this line should be looking for the parameters pattern

If Not (TypeOf(param[0]) = gb.boolean) Then

Testing for possible identifiable patterns for number of parameters passed

"Failure is the key to success; 
 each mistake teaches us something"  .. Morihei Ueshiba
Brian G


More information about the User mailing list