Otherway to have changing args on function than using table?

Discuss any general programming issues here

Otherway to have changing args on function than using table?

Postby Bugala » Fri Aug 18, 2017 10:06 pm

Here is what is not untypical to happen in my programs:
Code: Select all
function myfunc(xy_table)
x=nil
y=nil
if haveitem(xy_table, "x") then x = xy_table.x
if haveitem(xy_table, "y") then y = xy_table.y
endfunction

temptable = {x = 100}
myfunc(temptable)


What I would rather have is:
Code: Select all
function myfunc(x, y)
if x is there
   x = x
else
  x = nil
endif

if y is there
   y = y
else
   y = nil
endif
endfunction

myfunc(y)


But i guess this isnt really possible to do this way?
Bugala
 
Posts: 642
Joined: Sun Feb 14, 2010 8:11 pm

Re: Otherway to have changing args on function than using ta

Postby peceha » Fri Aug 18, 2017 11:55 pm

Code: Select all
function myfunc(xy_table)
x=rawGet(xy_table,x)
y=rawGet(xy_table,y)
endfunction

temptable = {x = 100}
myfunc(temptable)


Edit
forget :) that is not the answer to your question.
peceha
 
Posts: 110
Joined: Tue Dec 13, 2016 9:39 am
Location: Poland

Re: Otherway to have changing args on function than using ta

Postby SamuraiCrow » Sat Aug 19, 2017 11:14 pm

SamuraiCrow
 
Posts: 89
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Otherway to have changing args on function than using ta

Postby Bugala » Sun Aug 20, 2017 12:12 am

Im aware of that one SamuraiCrow. While that one gives possibility to have changing amount of variables, it doesnt give possibility to sometimes leave some arg from middle out.

To illustrate better what I am after and wanting:
Code: Select all
function MyFunc(varA, barB, varC) endfunction

Using instead:
Code: Select all
function myfunc(...) endfunction
I can have:
varA only, or varA and varB only, or varA, varB and varC all.

But what i cant have is to have varA and varC only, for:

Code: Select all
myfunc(sendingtovarA, sendingtovarC)

Now how does the function know that I am sending to varC instead of varB? It doesnt.


Basically what could be solution, but I dont think Hollywood has support for it, not sure if any programming language has, but basically i guess it would be doable that Hollywood would recognise what type of information I am sending to that function.

That say i have table Cat and Table Dog, which are different enough that you can make distinction between them, say:
Code: Select all
dog = { walk = walkfunc, sniff = snifffunc, wof = woffunc }
cat = { walk = walkfunc, meow = meowfunc, 9lives = true }


I guess in theory these two tables could be distingishable from each other since they have different subtables in them. Therefore there could be function:
Code: Select all
function myfunc(cat, dog) endfunction

and then if i would send only dog sometime:
Code: Select all
myfunc(mydog)

it could notice that the variable (table) that is coming now is not of a type cat but of a type dog and therefore send the sent variabletable to the cat variable.

But I guess this is not really doable in Hollywood (if even elsewhere), or is it?
Bugala
 
Posts: 642
Joined: Sun Feb 14, 2010 8:11 pm

Re: Otherway to have changing args on function than using ta

Postby Allanon » Sun Aug 20, 2017 8:21 am

Well your problems are complex...

You could try to make some more generic functions and pass list of objects/tables, each object could have a field that identify itself:

Your initial function:
Code: Select all
function myfunc(cat, dog) endfunction


you can prepare cat and dog as tables/objects, something like this:
Code: Select all
dog = { class = "dog", value = { ... your data here ... } }
cat= { class = "cat", value = { ... your data here ... } }


and myfunc could have a structure like this:
Code: Select all
function myfunc(data)
  for i, v In Pairs(data)
    Switch v.class
      Case "dog"
         ; Do stuff with v.value table that holds all dog variables
      Case "cat"
         ; Do stuff with v.value table that holds all cat variables
      Default
         ; Not recognized...
    EndSWitch
  next
endfunction


This way you can pass dog, cat, both of them or nothing at all:
Code: Select all
myfunc({
  { class = "dog", value = { color = "brown", speed = 10 }
    })

myfunc({
  { class = "dog", value = { color = "brown", speed = 10 },
  { class = "cat", value = { agility = 20, speed = 12 }
    })


Can this help?
User avatar
Allanon
 
Posts: 391
Joined: Sun Feb 14, 2010 8:53 pm
Location: Italy

Re: Otherway to have changing args on function than using ta

Postby Bugala » Sun Aug 20, 2017 10:33 am

Yes, that could be solution, not optimal but i guess best that is possible.

Also another idea that came to mind when not sending tables would be to use following:
Code: Select all
myfunc(varA, varAtype)
if varAtype = "dog" do dog stuff with varA
if varAtype = "cat" do cat stuff with varA
endfunction
Bugala
 
Posts: 642
Joined: Sun Feb 14, 2010 8:11 pm


Return to General programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron