[Gambas-user] "Groups" of optional arguments

BB adamnt42 at gmail.com
Thu Dec 1 03:32:58 CET 2022


These are both very good ideas. I have adopted a variant of both of them 
(mainly Lee's I changed the caller name to call "type" as many programs 
use the same invoker and one program may use different invokers).

Thanks guys, the spaghetti is now much less simpler to unravel.

bruce

On 1/12/22 6:02 am, Brian G wrote:
> ----- 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:
>>
>> 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
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----


More information about the User mailing list