how to fetch vararg's name and value?

General FreeBASIC programming questions.
oyster
Posts: 183
Joined: Oct 11, 2005 10:46

how to fetch vararg's name and value?

Postby oyster » Feb 01, 2019 5:55

I have read https://www.freebasic.net/forum/viewtopic.php?f=9&t=23905&p=222556&hilit=Vararg#p211115, but it is not I am searching for

In fact, I need something like this

Code: Select all

function fun(...)
    dim as string theName
    if "name" in Vararg then
        theName = name
    else
        theName = "admin"
    end if
   
    do something else
   
end function

so `fun(name="Mike")` will do something for "Mike", while fun() will do something for "admin"

I know I will meet the answer "why not to use default parameters"? But it is not a good solution. Please investigate this lua code

Code: Select all

iuptext{ name = "txtYMin", size = "38x10", value = Config.Ymin}

which will be translated into fb as something like this

Code: Select all

var text = iuptext(action=0)   ' no callback function is used
setAttribute (text, "name", "txtYMin")
setAttribute (text, "SIZE", "38x10")
setAttribute (text, "VALUE", str(Config.Ymin))

as the code said, many arguments in function call of iuptext are mapped to

Code: Select all

setAttribute (text, "ARG_NAME", str(ARG_VALUE))


There are 40 attributes for iuptext! It is too much, so it will be a disaster to write all the 40 attributes plus 1 callback function as the function default argument. Even we write such a function, the user will be lost to count on which place he should pass a value.

So what is the solution in fb? Thanks
Last edited by oyster on Feb 01, 2019 6:22, edited 1 time in total.
fxm
Posts: 9125
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: fetch vararg's name and value

Postby fxm » Feb 01, 2019 6:06

Why not group attributes in a Type, and pass only one instance of this Type?
oyster
Posts: 183
Joined: Oct 11, 2005 10:46

Re: fetch vararg's name and value

Postby oyster » Feb 01, 2019 6:09

more, for iuphbox, who has 16 attributes, then lua code

Code: Select all

iuphbox{
                iuplabel( title = "Pixel Split: " ),
                iuptext{ name = "txtSplit", value = Config.Split},
                alignment = "ACENTER"
           }

should be translated into

Code: Select all

var box = iuphbox(iuplabel( title = "Pixel Split: " ), iuptext( name = "txtSplit", value = Config.Split), NULL)
setAttributes(box, "alignment", "ACENTER")


at the same time

Code: Select all

iuphbox{
                iuplabel( title = "Pixel Split: " ),
                iuptext{ name = "txtSplit", value = Config.Split},
                iupbutton{ name = "btnOK", callback = evt_Ok},
                alignment = "ACENTER",
                MARGIN = "5x10"
           }

should be translated into

Code: Select all

var box = iuphbox(iuplabel( title = "Pixel Split: " ), _
                        iuptext( name = "txtSplit", value = Config.Split), _
                        iupbutton{ name = "btnOK", callback = evt_Ok}, _
                        NULL)
setAttributes(box, "alignment", "ACENTER")
setAttributes(box, "MARGIN", "5x10")


as you can find, the original C/FB iuphbox function accept a list of controls, or in other word, controls as varargs without var name. But at the same time, in the lua function, the attributes are varargs too but needs var name. How can we separate these 2 kinds of varargs in FB?

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests