can i store functions attributes to use them later?

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

can i store functions attributes to use them later?

Post by Bugala »

Here is a piece of test code that does do what I want basically:

Code: Select all

functable = {}

Function func1 (a, b)

InsertItem(functable, {func=func1, attributes={5, 4} })

functable[0].func(functable[0].attributes[0], functable[0].attributes[1])
What I am trying to do, is to add functions to a table. Idea being that when i execute them, it will also include attributes for that function.

In case I am using wrong word here, what I mean here is:

Code: Select all

 function checklocation(x, y)
Function is the "Checklocation()" by Attributes I mean "x" and "y".

In best case, I could just do like this:

Code: Select all

And it would result "CheckLocation(100, 200)" being executed.

Second best case would be:

Code: Select all

resulting in "CheckLocation(100, 200)" being executed.

That by other words, Attributestable[1] would contain "(100, 200)" inside it.

But is there any way to do it something like this, or do I have to resort into using the first, working example?

User avatar
Posts: 317
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: can i store functions attributes to use them later?

Post by Clyde »

I am not sure, but I think that your "best case" is not possible.

The second version could work IMHO. I have no Hollywood here currently, so this code may not work, but you get the idea:

Code: Select all

InsertItem(functtable, func1)
InsertItem(attributes, {5, 4})


BUT then you have to change the signature of func1 so that it takes just one argument:

Code: Select all

Function func1 (params)
But to be honest this seems not very handy or clear to read and understand.

I go with this approach. Not perfect, but better readable IMHO:

Code: Select all

Function _privateFunction(a, b)

funcTable = {

    publicFunction = Function(a, b)
        _privateFunction(a, b)

; now you call it

funcTable.publicFunction(a, b)

Of course, _privateFunction is not really private it is just a organisational/conceptual definition. Another disadvantage of this is that you have to change the signature at 4 places if it has to be changed.

But I see more pros and cons. But if anybody has a better way of doing it (Allanon?), then I would be happy to know.
Currently using: Hollywood 8 with Visual Studio Code and hw4vsc

Post Reply