Otherway to have changing args on function than using table?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Otherway to have changing args on function than using table?

Post by Bugala »

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?
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

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

Post by peceha »

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.
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

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

Post by SamuraiCrow »

I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

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

Post by Bugala »

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?
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

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

Post by Allanon »

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?
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

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

Post by Bugala »

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
Post Reply